diff options
author | mguetlein <martin.guetlein@gmail.com> | 2011-01-20 11:29:53 +0100 |
---|---|---|
committer | mguetlein <martin.guetlein@gmail.com> | 2011-01-20 11:29:53 +0100 |
commit | 9d06bd3024139f2bfee4722c7536ee4ffa99fe32 (patch) | |
tree | 727956c9d110bfc9774268cd7125b6e19d7175a1 /lib/error.rb | |
parent | 23d96df630689d122c023d76ec1d40d7688d2c96 (diff) |
implemented new error handling, still TODO rdf-support, replace halts
Diffstat (limited to 'lib/error.rb')
-rw-r--r-- | lib/error.rb | 60 |
1 files changed, 55 insertions, 5 deletions
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 |