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/overwrite.rb | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'lib/overwrite.rb') diff --git a/lib/overwrite.rb b/lib/overwrite.rb index 720ed77..2f9fabd 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -10,30 +10,30 @@ before { request.env['HTTP_ACCEPT'] += ";text/html" if request.env["HTTP_USER_AGENT"]=~/MSIE/ } -# handle errors manually -# this is to return 502, when an error occurs during a rest-call (see rest_client_wrapper.rb) -set :raise_errors, Proc.new { false } -set :show_exceptions, false -error do - # try if the error is an OpenTox::Error - if OpenTox::Error.parse(request.env['sinatra.error'].to_s) - # if true, this error comes from rest_client_wrapper, halt with 502 - # (502 is defined in OT API as Error coming from other service) - halt 502,request.env['sinatra.error'] +# Error handling +# Errors are logged as error and formated according to acccept-header +# Non OpenTox::Errors (defined in error.rb) are handled as internal error (500), stacktrace is logged +# IMPT: set sinatra settings :show_exceptions + :raise_errors to false in config.ru, otherwise Rack::Showexceptions takes over +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 - # else, raise exception, this will return 500 = internal error - raise request.env['sinatra.error'] - end -end - -class Sinatra::Base - # overwriting halt to log halts (!= 202) - def halt(*response) - LOGGER.error "halt "+response.first.to_s+" "+(response.size>1 ? response[1].to_s : "") if response and response.first and response.first >= 300 - # orig sinatra code: - response = response.first if response.length == 1 - throw :halt, response + # (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) end class String -- cgit v1.2.3