Skip to content

Commit 03f4937

Browse files
authored
Merge pull request #994 from travisofthenorth/store-exceptions
Store exceptions in request env
2 parents 7ebba9b + ae71cab commit 03f4937

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ def handle_exceptions(e)
284284
}
285285
end
286286

287+
# Store exception for other middlewares
288+
request.env['action_dispatch.exception'] ||= e
289+
287290
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
288291
Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
289292
errors = internal_server_error.errors

test/controllers/controller_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ def test_whitelist_all_exceptions
104104
JSONAPI.configuration.whitelist_all_exceptions = original_config
105105
end
106106

107+
def test_exception_added_to_request_env
108+
original_config = JSONAPI.configuration.whitelist_all_exceptions
109+
$PostProcessorRaisesErrors = true
110+
refute @request.env['action_dispatch.exception']
111+
assert_cacheable_get :index
112+
assert @request.env['action_dispatch.exception']
113+
114+
JSONAPI.configuration.whitelist_all_exceptions = true
115+
assert_cacheable_get :index
116+
assert @request.env['action_dispatch.exception']
117+
ensure
118+
$PostProcessorRaisesErrors = false
119+
JSONAPI.configuration.whitelist_all_exceptions = original_config
120+
end
121+
107122
def test_exception_includes_backtrace_when_enabled
108123
original_config = JSONAPI.configuration.include_backtraces_in_errors
109124
$PostProcessorRaisesErrors = true

0 commit comments

Comments
 (0)