Skip to content

Commit f9a7ef8

Browse files
committed
Test that #report sets the cause
1 parent 9253e6e commit f9a7ef8

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

activesupport/lib/active_support/error_reporter.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,12 @@ def report(error, handled: true, severity: handled ? :warning : :error, context:
276276

277277
private
278278
def ensure_backtrace(error)
279-
return if error.frozen? # re-raising won't add a backtrace
279+
return if error.frozen? # re-raising won't add a backtrace or set the cause
280280
return unless error.backtrace.nil?
281281

282282
begin
283-
# We could use Exception#set_backtrace, but until Ruby 3.4
284-
# it only support setting `Exception#backtrace` and not
285-
# `Exception#backtrace_locations`. So raising the exception
286-
# is a good way to build a real backtrace.
283+
# As of Ruby 3.4, we could use Exception#set_backtrace to set the backtrace,
284+
# but there's nothing like Exception#set_cause. Raising+rescuing is the only way to set the cause.
287285
raise error
288286
rescue error.class => error
289287
end

activesupport/test/error_reporter_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ class ErrorReporterTest < ActiveSupport::TestCase
172172
assert_equal __FILE__, error.backtrace_locations.first.path
173173
end
174174

175+
test "#report assigns a cause if it's missing" do
176+
raise "the original cause"
177+
rescue => cause
178+
new_error = StandardError.new("A new error that should wrap the StandardError")
179+
assert_nil new_error.cause
180+
181+
@reporter.report(new_error)
182+
183+
assert_same cause, new_error.cause
184+
end
185+
175186
test "#record passes through the return value" do
176187
result = @reporter.record do
177188
2 + 2

0 commit comments

Comments
 (0)