From c72910a8bf782c10662dd4392e81ef6408f801ee Mon Sep 17 00:00:00 2001 From: Felix Gilcher Date: Fri, 1 Feb 2013 09:42:02 +0000 Subject: [PATCH] log fatal errors that we catch In case we rescue from a fatal error, we want the error and the backtrace to the error logged, so we can debug later on. This change injects the configured logger from the rails app to the grape API and logs error as well as backtrace in a rails-like fashion. --- config/routes.rb | 1 + lib/api.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 7ffa081a..66cb62bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Gitlab::Application.routes.draw do # API require 'api' + Gitlab::API.logger Rails.logger mount Gitlab::API => '/api' constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } diff --git a/lib/api.rb b/lib/api.rb index 3dd82715..15d99cc7 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -8,7 +8,16 @@ module Gitlab rack_response({'message' => '404 Not found'}.to_json, 404) end - rescue_from :all do + rescue_from :all do |exception| + # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60 + # why is this not wrapped in something reusable? + trace = exception.backtrace + + message = "\n#{exception.class} (#{exception.message}):\n" + message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) + message << " " << trace.join("\n ") + + API.logger.add Logger::FATAL, message rack_response({'message' => '500 Internal Server Error'}, 500) end