Commit c72910a8bf782c10662dd4392e81ef6408f801ee

Authored by Felix Gilcher
1 parent 2a669fc8

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.
Showing 2 changed files with 11 additions and 1 deletions   Show diff stats
config/routes.rb
@@ -8,6 +8,7 @@ Gitlab::Application.routes.draw do @@ -8,6 +8,7 @@ Gitlab::Application.routes.draw do
8 8
9 # API 9 # API
10 require 'api' 10 require 'api'
  11 + Gitlab::API.logger Rails.logger
11 mount Gitlab::API => '/api' 12 mount Gitlab::API => '/api'
12 13
13 constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } 14 constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? }
@@ -8,7 +8,16 @@ module Gitlab @@ -8,7 +8,16 @@ module Gitlab
8 rack_response({'message' => '404 Not found'}.to_json, 404) 8 rack_response({'message' => '404 Not found'}.to_json, 404)
9 end 9 end
10 10
11 - rescue_from :all do 11 + rescue_from :all do |exception|
  12 + # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
  13 + # why is this not wrapped in something reusable?
  14 + trace = exception.backtrace
  15 +
  16 + message = "\n#{exception.class} (#{exception.message}):\n"
  17 + message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
  18 + message << " " << trace.join("\n ")
  19 +
  20 + API.logger.add Logger::FATAL, message
12 rack_response({'message' => '500 Internal Server Error'}, 500) 21 rack_response({'message' => '500 Internal Server Error'}, 500)
13 end 22 end
14 23