Skip to content

Commit f7774e8

Browse files
author
Simon Coffey
committedOct 19, 2017
Clarify output when printing nested exception traces
Since v10.2.0, if an exception has a nested cause exception, the cause is also displayed in the trace output.[1] For heavily-nested exceptions, this output can be quite lengthy - for example, Rails migrations nest DB errors twice over, resulting in an error message and backtrace repeated three times. To break up this output and make it clearer what each individual backtrace relates to, this adds whitespace and a "Caused by:" label to each nested exception being displayed. To prevent "Caused by:" labels occurring on their own, I've moved the exception loop shortcut return into the `#display_cause_details` method. This doesn't alter the behaviour of the shortcut, as only the first exception will be unconditionally printed (which was already the case, as the first exception can't be already seen). [1] fbb22e7 [2] 57c932c
1 parent 99f48f2 commit f7774e8

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎lib/rake/application.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,22 @@ def display_error_message(ex) # :nodoc:
207207
end
208208

209209
def display_exception_details(ex) # :nodoc:
210-
seen = Thread.current[:rake_display_exception_details_seen] ||= []
211-
return if seen.include? ex
212-
seen << ex
210+
display_exception_details_seen << ex
213211

214212
display_exception_message_details(ex)
215213
display_exception_backtrace(ex)
216-
display_exception_details(ex.cause) if has_cause?(ex)
214+
display_cause_details(ex.cause) if has_cause?(ex)
215+
end
216+
217+
def display_cause_details(ex) # :nodoc:
218+
return if display_exception_details_seen.include? ex
219+
220+
trace "\nCaused by:"
221+
display_exception_details(ex)
222+
end
223+
224+
def display_exception_details_seen # :nodoc:
225+
Thread.current[:rake_display_exception_details_seen] ||= []
217226
end
218227

219228
def has_cause?(ex) # :nodoc:

‎test/test_rake_application.rb

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def test_display_exception_details_cause
9797

9898
assert_empty out
9999

100+
assert_match "Caused by:", err
100101
assert_match "cause a", err
101102
assert_match "cause b", err
102103
end

0 commit comments

Comments
 (0)