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
Which is the proper order to handle exception.cause in ActiveSupport::Rescuable#rescue_from ? #29739
Comments
I'm having the same trouble with nested exceptions. We upgraded to Rails 5 last week; this was a surprising change. We're running 5.0.4 under Ruby 2.4.1. Upgrading to 5.1.2 seemed to have no effect. |
From #25018 (comment):
So, the general change was indeed deliberate. But in the specific case of
AFAICS, this situation will only manifest if there's a more generic handler defined that catches If you're up for a PR to do the above, it sounds like a reasonable change. @TurtleKitty how closely does your situation match the one @rinmu described? |
Fairly similar. I have some code that did I've had to write a new handler that rescues from StandardError and then recurses through the exception causes to find the real reason. |
Thanks for your comments. Please check PR #30601. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
For those who land here in 2020+, this is a super annoying issue in Ruby 2.1+ / Rails 5+ when you have a structured set of custom exceptions in your app and a fallback to catching StandardError in Application controller. Here is our solution:
|
Summary
Since Rails 5.0, the order in which handler is applied for exceptions including cause has been changed.
It looks like a deliberate change as written in the #25018 comment, but I think there are at least the following problems.
Probrem
if Article with
id==10
does not exist, handle_ 404 was executed in Rails 4.2.8,Since Rails 5.0.0, handle_ 500 is now executed.
Errors occurring in view are wrapped in ActionView::Template::Error.
In Rails 4.2.8, when error has cause, It looked for a handler that matched
exception.cause
.For Rails 5.0.0 and later, the
exception.cause
check is done only if the wrapping error does not match any handler.I think the behavior before 4.2.8 is preferable in the above case.
I know that shouldn't do
rescue_from StandardError
be listed in RailsGuide.This may not be an appropriate example.
However, I think that we always encounter this problem when we need to reference cause and want to deal with special errors and generic errors in order.
Solution
How about restoring the logic of #24158 to activesupport/lib/active_support/rescuable.rb ?
System configuration
Rails version:
4.2.8, 5.0.0, 5.1.2
Ruby version:
2.3.1
The text was updated successfully, but these errors were encountered: