Skip to content

Commit

Permalink
Clarify output when printing nested exception traces
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Simon Coffey committed Oct 19, 2017
1 parent 99f48f2 commit 71b02b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/rake/application.rb
Expand Up @@ -207,13 +207,22 @@ def display_error_message(ex) # :nodoc:
end

def display_exception_details(ex) # :nodoc:
seen = Thread.current[:rake_display_exception_details_seen] ||= []
return if seen.include? ex
seen << ex
display_exception_details_seen << ex

display_exception_message_details(ex)
display_exception_backtrace(ex)
display_exception_details(ex.cause) if has_cause?(ex)
display_cause_details(ex.cause) if has_cause?(ex)
end

def display_cause_details(ex) # :nodoc:
return if display_exception_details_seen.include? ex

trace "\nCaused by:"
display_exception_details(ex)
end

def display_exception_details_seen
Thread.current[:rake_display_exception_details_seen] ||= []
end

def has_cause?(ex) # :nodoc:
Expand Down
1 change: 1 addition & 0 deletions test/test_rake_application.rb
Expand Up @@ -97,6 +97,7 @@ def test_display_exception_details_cause

assert_empty out

assert_match "Caused by:", err
assert_match "cause a", err
assert_match "cause b", err
end
Expand Down

0 comments on commit 71b02b5

Please sign in to comment.