From 9d06bd3024139f2bfee4722c7536ee4ffa99fe32 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Thu, 20 Jan 2011 11:29:53 +0100 Subject: implemented new error handling, still TODO rdf-support, replace halts --- lib/error.rb | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) (limited to 'lib/error.rb') diff --git a/lib/error.rb b/lib/error.rb index 87e1a5d..b72ce7e 100644 --- a/lib/error.rb +++ b/lib/error.rb @@ -1,11 +1,61 @@ -module OpenTox - class NotFoundError < RuntimeError - +# adding additional fields to Exception class to format errors according to OT-API +class Exception + attr_accessor :creator, :errorCause, :id +end + +module OpenTox + + class NotAuthorizedError < Exception end - class BadRequestError < RuntimeError - + class NotFoundError < Exception end + class BadRequestError < Exception + end + + class RestCallError < Exception + attr_accessor :code, :body, :uri, :payload, :headers + end + + class ErrorReport + + # formats error according to accept-header, yaml is default + # ( sets content-type in response accordingly ) + # @param [Exception] error + # @param |Sinatra::Request, optional] request + # @param [Sinatra::Response, optiona,] response, optional to set content-type + # @return [String] formated error + def self.format(error, request=nil, response=nil) + # sets current uri + error.creator = "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}" if request + accept = request.env['HTTP_ACCEPT'].to_s if request + case accept + # when /rdf/ + # TODO add error to rdf + when /html/ + response['Content-Type'] = 'text/html' if response + OpenTox.text_to_html error.to_yaml + else + response['Content-Type'] = 'application/x-yaml' if response + error.to_yaml + end + end + + # trys to parse error from text + # @return [Exception] Exception if parsing sucessfull, nil otherwise + def self.parse( body ) + begin + err = YAML.load(body) + if err and err.is_a?(Exception) + return err + else + return nil + end + rescue + return nil + end + end + end end \ No newline at end of file -- cgit v1.2.3