Skip to content

Commit f48bb1b

Browse files
committed
Call fallback exception handlers with the right exception
The issue presented in rails#26246 showed a deeper underlying problem. When we fell back to the exception handler for an exceptions cause, we were calling that handler with the outer raised exception. This breaks the calling code's expectations, especially if the exception has methods on it behond those from `StandardError`.
1 parent 8d015df commit f48bb1b

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

activesupport/lib/active_support/rescuable.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ def rescue_from(*klasses, with: nil, &block)
8585
#
8686
# Returns the exception if it was handled and +nil+ if it was not.
8787
def rescue_with_handler(exception, object: self)
88-
if handler = handler_for_rescue(exception, object: object)
88+
handler, exception = handler_for_rescue(exception, object: object)
89+
if handler
8990
handler.call exception
9091
exception
9192
end
9293
end
9394

9495
def handler_for_rescue(exception, object: self) #:nodoc:
95-
case rescuer = find_rescue_handler(exception)
96+
rescuer, exception = find_rescue_handler(exception)
97+
result = case rescuer
9698
when Symbol
9799
method = object.method(rescuer)
98100
if method.arity == 0
@@ -107,6 +109,7 @@ def handler_for_rescue(exception, object: self) #:nodoc:
107109
-> e { object.instance_exec(e, &rescuer) }
108110
end
109111
end
112+
[result, exception]
110113
end
111114

112115
private
@@ -121,7 +124,11 @@ def find_rescue_handler(exception)
121124
end
122125
end
123126

124-
handler || find_rescue_handler(exception.cause)
127+
if handler
128+
[handler, exception]
129+
else
130+
find_rescue_handler(exception.cause)
131+
end
125132
end
126133
end
127134

activesupport/test/rescuable_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@ def test_children_should_inherit_rescue_definitions_from_parents_and_child_rescu
137137

138138
def test_rescue_falls_back_to_exception_cause
139139
@stargate.dispatch :fall_back_to_cause
140-
assert_equal "unhandled RuntimeError with a handleable cause", @stargate.result
140+
assert_equal "dex", @stargate.result
141141
end
142142
end

0 commit comments

Comments
 (0)