1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

commands/wtf,pry: add support for Exception#cause

Fixes #1449 (support exceptions with cause)
Replaces #1525
This commit is contained in:
Aleksandar Kostadinov 2016-03-01 23:59:30 +02:00 committed by Kyrylo Silin
parent ef80932bc6
commit 30cca64e5a
3 changed files with 61 additions and 0 deletions

View file

@ -46,6 +46,15 @@ class Pry
else else
output.puts "#{exception.class}: #{exception.message}" output.puts "#{exception.class}: #{exception.message}"
output.puts "from #{exception.backtrace.first}" 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
end end

View file

@ -32,6 +32,19 @@ class Pry
else else
output.puts with_line_numbers(backtrace.first(size_of_backtrace)) output.puts with_line_numbers(backtrace.first(size_of_backtrace))
end 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 end
private private

39
spec/commands/wtf_spec.rb Normal file
View file

@ -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