Skip to content

Respect action_dispatch.log_rescued_responses #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Copy link
Author

@maxjacobson maxjacobson Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #179, there was a concern about the rescue_responses? method not working in Rails 5 and 6, which led to this respond_to? check being added. I don't believe it's necessary, however, because this is within a conditional that checks (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7, so we can feel confident that we are in Rails 7.1+. Given that, I took the liberty of removing the respond_to? check


level = request.get_header("action_dispatch.debug_exception_log_level")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this more aligned with the default behavior of ActionDispatch::DebugExceptions makes sense to me. I don't think this is a breaking change, because the previous behavior was merged to the master branch, but never released. Let me know if you'd like me to remove this part though.

ActionController::Base.logger.log(level, wrapper.exception)
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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