diff --git a/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb b/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb index 9cc3661..0e08ebd 100644 --- a/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb +++ b/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb @@ -7,9 +7,11 @@ class DebugExceptions undef_method :log_error if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7 - def log_error(_request, wrapper) + def log_error(request, wrapper) Rails.application.deprecators.silence do - level = wrapper.respond_to?(:rescue_response?) && wrapper.rescue_response? ? :debug : :fatal + return if !log_rescued_responses?(request) && wrapper.rescue_response? + + level = request.get_header("action_dispatch.debug_exception_log_level") ActionController::Base.logger.log(level, wrapper.exception) end end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb index 912f796..48db940 100644 --- a/test/controllers/articles_controller_test.rb +++ b/test/controllers/articles_controller_test.rb @@ -141,6 +141,26 @@ class ArticlesControllerTest < ActionDispatch::IntegrationTest assert_equal 4, messages.count, messages assert_kind_of ActiveRecord::RecordNotFound, messages[3].exception end + + it "raises and does not log exception when action_dispatch.log_rescued_responses is false" do + # we're testing ActionDispatch::DebugExceptions here too + messages = semantic_logger_events do + old_show = Rails.application.env_config["action_dispatch.show_exceptions"] + old_log_rescued_responses = Rails.application.env_config["action_dispatch.log_rescued_responses"] + + begin + Rails.application.env_config["action_dispatch.show_exceptions"] = :all + Rails.application.env_config["action_dispatch.log_rescued_responses"] = false + get article_url(:show) + rescue ActiveRecord::RecordNotFound => e + # expected + ensure + Rails.application.env_config["action_dispatch.show_exceptions"] = old_show + Rails.application.env_config["action_dispatch.log_rescued_responses"] = old_log_rescued_responses + end + end + assert_equal 3, messages.count, messages + end end end end