这是indexloc提供的服务,不要输入任何密码
Skip to content

Log when rescue_from handles an exception #55389

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: main
Choose a base branch
from

Conversation

swebb
Copy link

@swebb swebb commented Jul 23, 2025

Motivation / Background

In development when a before_action renders or redirects this error message appears on the logs:

Filter chain halted as :before_action rendered or redirected

I find this message very helpful. However, if a before_action raises an error that is handled by a rescue_from block, nothing is logged indicating an error occurred.

Original proposal

Detail

This PR adds a log message indicating an exception was rescued, and displays where it was originally raised. For example:

rescue_from handled exception HomeController::MyError (Oh no!) -
/home/vscode/my-test-app/app/controllers/home_controller.rb:15:in
'HomeController#authenticate'

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

In development when a `before_action` renders or redirects this error
message appears on the logs:

> Filter chain halted as :before_action rendered or redirected

I find this message very helpful. However, if a before_action raises an
error that is handled by a rescue_from block, nothing is logged
indicating an error occurred.

This PR adds a log message indicating an exception was rescued, and
displays where it was originally raised. For example:

> rescue_from handled exception HomeController::MyError("Oh no!") -
>   /home/vscode/my-test-app/app/controllers/home_controller.rb:15:in
>   'HomeController#authenticate'
@swebb swebb force-pushed the log_rescue_from branch from a48c863 to af8c732 Compare July 23, 2025 14:22
@@ -92,6 +92,7 @@ def rescue_with_handler(exception, object: self, visited_exceptions: [])

if handler = handler_for_rescue(exception, object: object)
handler.call exception
logger.info("rescue_from handled exception #{exception.class} (#{exception.message}) - #{exception.backtrace.first}")
Copy link
Member

Choose a reason for hiding this comment

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

I think the feature makes sense, but this isn't the proper way to implement it.

The log should be in actionpack/lib/action_controller/log_subscriber.rb, and here it should just fire an ActiveSupport::Notification.

You can look at def halted_callback_hook for inspiration.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @byroot. I'll check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants