From 0e759bee80e2668e1fec7b741a4ea18015f98b84 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 21 Jan 2011 11:52:53 +0100 Subject: simplify error handling once again, and adding http code --- lib/error.rb | 18 +++++++++++++----- lib/overwrite.rb | 19 +++---------------- lib/rest_client_wrapper.rb | 10 +++++----- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/lib/error.rb b/lib/error.rb index b72ce7e..e47ad62 100644 --- a/lib/error.rb +++ b/lib/error.rb @@ -1,22 +1,27 @@ # adding additional fields to Exception class to format errors according to OT-API class Exception - attr_accessor :creator, :errorCause, :id + attr_accessor :creator, :errorCause, :id, :http_code + def http_code; 500; end end module OpenTox - class NotAuthorizedError < Exception + class BadRequestError < Exception + def http_code; 400; end end - class NotFoundError < Exception + class NotAuthorizedError < Exception + def http_code; 401; end end - class BadRequestError < Exception + class NotFoundError < Exception + def http_code; 404; end end class RestCallError < Exception - attr_accessor :code, :body, :uri, :payload, :headers + attr_accessor :rest_code, :rest_body, :rest_uri, :rest_payload, :rest_headers + def http_code; 502; end end class ErrorReport @@ -30,6 +35,9 @@ module OpenTox 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 + # bit of a hack: set instance attribute in order to add it for the to_yaml conversion + error.http_code = error.http_code + accept = request.env['HTTP_ACCEPT'].to_s if request case accept # when /rdf/ diff --git a/lib/overwrite.rb b/lib/overwrite.rb index 2f9fabd..83d8099 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -18,22 +18,9 @@ error Exception do error = request.env['sinatra.error'] # log error to logfile LOGGER.error error.class.to_s+": "+error.message - case error.class - when OpenTox::BadRequestError - code = 400 - when OpenTox::NotAuthorizedError - code = 401 - when OpenTox::NotFoundError - code = 404 - when OpenTox::RestCallError - code = 502 - else - # (unwanted RuntimeExceptions as well as sth. like 'raise "invalid state"' is handled here) - code = 500 - # log backtrace for debugging - LOGGER.error error.backtrace.join("\n") - end - halt code,OpenTox::ErrorReport.format(error,request,response) + # log backtrace only if code is 500 -> unwanted (Runtime)Exceptions and internal errors (see error.rb) + LOGGER.error error.backtrace.join("\n") if error.http_code==500 + halt error.http_code,OpenTox::ErrorReport.format(error,request,response) end class String diff --git a/lib/rest_client_wrapper.rb b/lib/rest_client_wrapper.rb index 5bc8072..3d7b72e 100644 --- a/lib/rest_client_wrapper.rb +++ b/lib/rest_client_wrapper.rb @@ -131,15 +131,15 @@ module OpenTox def self.raise_ot_error( code, message, body, uri, headers, payload=nil ) error = OpenTox::RestCallError.new("REST call returned error: '"+message.to_s+"'") - error.code = code - error.uri = uri - error.headers = headers - error.payload = payload + error.rest_code = code + error.rest_uri = uri + error.rest_headers = headers + error.rest_payload = payload parsed = OpenTox::ErrorReport.parse(body) if body if parsed error.errorCause = parsed else - error.body = body + error.rest_body = body end raise error end -- cgit v1.2.3