Skip to content
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

Clarify output when printing nested exception traces #232

Merged
merged 1 commit into from Oct 24, 2017

Conversation

urbanautomaton
Copy link
Contributor

@urbanautomaton urbanautomaton commented Oct 19, 2017

Since v10.2.0, if an exception has a nested cause exception, the cause is also displayed in the trace output.

For deeply-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. With long backtraces, this output can be difficult to visually scan.

To break up the output and make it clearer what each individual backtrace relates to, this PR adds whitespace and a "Caused by:" label to each nested exception being displayed.

Given the following Rakefile:

task :oops do
  begin
    raise StandardError, "oops"
  rescue => a
    begin
      raise ArgumentError, "didn't like that", cause: a
    rescue => b
      raise SyntaxError, "why not eh", cause: b
    end
  end
end

This changes the output from this in current master:

$ rake oops
rake aborted!
SyntaxError: why not eh
/Users/simon/dev/clones/rake/Rakefile:8:in `rescue in rescue in block in <top (required)>'
/Users/simon/dev/clones/rake/Rakefile:5:in `rescue in block in <top (required)>'
/Users/simon/dev/clones/rake/Rakefile:2:in `block in <top (required)>'
/Users/simon/.gem/ruby/2.4.2/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
ArgumentError: didn't like that
/Users/simon/dev/clones/rake/Rakefile:6:in `rescue in block in <top (required)>'
/Users/simon/dev/clones/rake/Rakefile:2:in `block in <top (required)>'
/Users/simon/.gem/ruby/2.4.2/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
StandardError: oops
/Users/simon/dev/clones/rake/Rakefile:3:in `block in <top (required)>'
/Users/simon/.gem/ruby/2.4.2/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => oops
(See full trace by running task with --trace)

To this:

$ rake oops
rake aborted!
SyntaxError: why not eh
/Users/simon/dev/clones/rake/Rakefile:8:in `rescue in rescue in block in <top (required)>'
/Users/simon/dev/clones/rake/Rakefile:5:in `rescue in block in <top (required)>'
/Users/simon/dev/clones/rake/Rakefile:2:in `block in <top (required)>'
/Users/simon/dev/clones/rake/exe/rake:27:in `<top (required)>'

Caused by:
ArgumentError: didn't like that
/Users/simon/dev/clones/rake/Rakefile:6:in `rescue in block in <top (required)>'
/Users/simon/dev/clones/rake/Rakefile:2:in `block in <top (required)>'
/Users/simon/dev/clones/rake/exe/rake:27:in `<top (required)>'

Caused by:
StandardError: oops
/Users/simon/dev/clones/rake/Rakefile:3:in `block in <top (required)>'
/Users/simon/dev/clones/rake/exe/rake:27:in `<top (required)>'
Tasks: TOP => oops
(See full trace by running task with --trace)

Is this a change you'd be interested in merging? If so, feedback on both the output and implementation would be very welcome, and I'd be happy to make any changes necessary.

Cheers,
Simon

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] ruby@fbb22e7
[2] ruby@57c932c
@hsbt
Copy link
Member

hsbt commented Oct 24, 2017

@urbanautomaton Thanks for your suggestion. This pull-request makes clarify problem of exceptions for users.

@hsbt hsbt merged commit 920ead3 into ruby:master Oct 24, 2017
@urbanautomaton
Copy link
Contributor Author

Thank you! ❤️

@urbanautomaton urbanautomaton deleted the clarify-exception-cause-output branch October 24, 2017 21:46
@hsbt
Copy link
Member

hsbt commented Oct 25, 2017

I released rake-12.2.0 contained your pull request. Please try it :octocat:

@urbanautomaton
Copy link
Contributor Author

That's great - all working, thanks again. :shipit:

This was referenced Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants