summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2018-10-10 00:29:56 +0800
committerGitHub <noreply@github.com>2018-10-10 00:29:56 +0800
commit528ff64806b0b55d85c8b47f3ab58c1e8c1d1499 (patch)
tree465d6f11f23f589ce39140ed7070a6afa18297f9
parentef80932bc66ca8a03f970d24280a77af30293489 (diff)
parent30cca64e5a75d1efb62b558c018d7ab44492b02a (diff)
downloadpry-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.rb9
-rw-r--r--lib/pry/commands/wtf.rb13
-rw-r--r--spec/commands/wtf_spec.rb39
3 files changed, 61 insertions, 0 deletions
diff --git a/lib/pry.rb b/lib/pry.rb
index a0dabd3a..9fee588f 100644
--- a/lib/pry.rb
+++ b/lib/pry.rb
@@ -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