Skip to content

Commit a38f181

Browse files
committed
Log errors captured by the JSON api
One thing that we were lacking was actually logging any errors which occurred with a JSON api endpoint. This simply cargo cults, copies, the logic that Rails already uses for errors. The logic has changed slightly for Rails 5, but since we are not targeting that just yet we'll stick with the latest Rails 4.2.6 version. We need to log the error before we render the JSON as our middleware's return value from `call` is used as the response. There isn't much special to note about the logging itself. All of the complex logic comes from Rails, mostly to ensure it focuses on app related errors, stripping out Rails framework details, when possible.
1 parent 6038a2d commit a38f181

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

lib/kracken/json_api/public_exceptions.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
module Kracken
22
module JsonApi
3+
# Error logging methods used by `ActionDispatch::DebugExceptions`
4+
# https://github.com/rails/rails/blob/v4.2.6/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5+
module ErrorLogging
6+
def log_error(env, wrapper)
7+
logger = logger(env)
8+
return unless logger
9+
10+
exception = wrapper.exception
11+
12+
trace = wrapper.application_trace
13+
trace = wrapper.framework_trace if trace.empty?
14+
15+
ActiveSupport::Deprecation.silence do
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+
logger.fatal("#{message}\n\n")
20+
end
21+
end
22+
23+
def logger(env)
24+
env['action_dispatch.logger'] || stderr_logger
25+
end
26+
27+
def stderr_logger
28+
@stderr_logger ||= ActiveSupport::Logger.new($stderr)
29+
end
30+
end
31+
332
class PublicExceptions
4-
attr_reader :app
33+
include ErrorLogging
34+
535
def initialize(app)
636
@app = app
737
end
@@ -29,7 +59,9 @@ def capture_error(env)
2959

3060
response
3161
rescue Exception => exception
32-
render_json_error(ExceptionWrapper.new(env, exception))
62+
wrapper = ExceptionWrapper.new(env, exception)
63+
log_error(env, wrapper)
64+
render_json_error(wrapper)
3365
end
3466

3567
if Rails.env.production?

0 commit comments

Comments
 (0)