diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2018-10-10 00:29:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-10 00:29:56 +0800 |
commit | 528ff64806b0b55d85c8b47f3ab58c1e8c1d1499 (patch) | |
tree | 465d6f11f23f589ce39140ed7070a6afa18297f9 | |
parent | ef80932bc66ca8a03f970d24280a77af30293489 (diff) | |
parent | 30cca64e5a75d1efb62b558c018d7ab44492b02a (diff) | |
download | pry-528ff64806b0b55d85c8b47f3ab58c1e8c1d1499.tar.gz |
Merge pull request #1791 from pry/1449-wtf-cause
commands/wtf,pry: add support for Exception#cause
-rw-r--r-- | lib/pry.rb | 9 | ||||
-rw-r--r-- | lib/pry/commands/wtf.rb | 13 | ||||
-rw-r--r-- | spec/commands/wtf_spec.rb | 39 |
3 files changed, 61 insertions, 0 deletions
@@ -46,6 +46,15 @@ class Pry else output.puts "#{exception.class}: #{exception.message}" output.puts "from #{exception.backtrace.first}" + + if exception.respond_to? :cause + cause = exception.cause + while cause + output.puts "Caused by #{cause.class}: #{cause}\n" + output.puts "from #{cause.backtrace.first}" + cause = cause.cause + end + end end end diff --git a/lib/pry/commands/wtf.rb b/lib/pry/commands/wtf.rb index d28d64cb..43950a33 100644 --- a/lib/pry/commands/wtf.rb +++ b/lib/pry/commands/wtf.rb @@ -32,6 +32,19 @@ class Pry else output.puts with_line_numbers(backtrace.first(size_of_backtrace)) end + + if exception.respond_to? :cause + cause = exception.cause + while cause + output.puts "#{text.bold('Caused by:')} #{cause.class}: #{cause}\n--" + if opts.verbose? + output.puts with_line_numbers(cause.backtrace) + else + output.puts with_line_numbers(cause.backtrace.first(size_of_backtrace)) + end + cause = cause.cause + end + end end private diff --git a/spec/commands/wtf_spec.rb b/spec/commands/wtf_spec.rb new file mode 100644 index 00000000..ee77906a --- /dev/null +++ b/spec/commands/wtf_spec.rb @@ -0,0 +1,39 @@ +require_relative '../helper' + +describe "wtf?!" do + let(:tester) do + pry_tester do + def last_exception=(ex) + @pry.last_exception = ex + end + + def last_exception + @pry.last_exception + end + end + end + + it "unwinds nested exceptions" do + if Gem::Version.new(RUBY_VERSION) <= Gem::Version.new('2.0.0') + skip('Exception#cause is not supported') + end + + begin + begin + begin + raise 'inner' + rescue RuntimeError + raise 'outer' + end + end + rescue RuntimeError => ex + tester.last_exception = ex + end + + expect(tester.eval('wtf -v')).to match(/ + Exception:\sRuntimeError:\souter + .+ + Caused\sby:\sRuntimeError:\sinner + /xm) + end +end |