From ae71cab83bb8a628a6dd5422e603310a96c43b2e Mon Sep 17 00:00:00 2001 From: Travis Hunter Date: Wed, 1 Mar 2017 12:24:43 -0500 Subject: [PATCH] Store exceptions in request env --- lib/jsonapi/acts_as_resource_controller.rb | 3 +++ test/controllers/controller_test.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/jsonapi/acts_as_resource_controller.rb b/lib/jsonapi/acts_as_resource_controller.rb index bc0229ddc..830109df5 100644 --- a/lib/jsonapi/acts_as_resource_controller.rb +++ b/lib/jsonapi/acts_as_resource_controller.rb @@ -284,6 +284,9 @@ def handle_exceptions(e) } end + # Store exception for other middlewares + request.env['action_dispatch.exception'] ||= e + internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e) Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" } errors = internal_server_error.errors diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index b95dafbbf..20fa914f6 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -104,6 +104,21 @@ def test_whitelist_all_exceptions JSONAPI.configuration.whitelist_all_exceptions = original_config end + def test_exception_added_to_request_env + original_config = JSONAPI.configuration.whitelist_all_exceptions + $PostProcessorRaisesErrors = true + refute @request.env['action_dispatch.exception'] + assert_cacheable_get :index + assert @request.env['action_dispatch.exception'] + + JSONAPI.configuration.whitelist_all_exceptions = true + assert_cacheable_get :index + assert @request.env['action_dispatch.exception'] + ensure + $PostProcessorRaisesErrors = false + JSONAPI.configuration.whitelist_all_exceptions = original_config + end + def test_exception_includes_backtrace_when_enabled original_config = JSONAPI.configuration.include_backtraces_in_errors $PostProcessorRaisesErrors = true