Skip to content

Commit

Permalink
Print exception cause when inspecting with backtrace (#5833)
Browse files Browse the repository at this point in the history
  • Loading branch information
RX14 committed Mar 31, 2018
1 parent 945557b commit 49d722c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/std/exception_spec.cr
Expand Up @@ -18,4 +18,20 @@ describe "Exception" do
ex = FooError.new("foo?")
ex.inspect.should eq("#<FooError:foo? -- bar!>")
end

it "inspects with cause" do
cause = Exception.new("inner")
ex = expect_raises(Exception, "wrapper") do
begin
raise cause
rescue ex
raise Exception.new("wrapper", cause: ex)
end
end

ex.cause.should be(cause)
ex.inspect_with_backtrace.should contain("wrapper")
ex.inspect_with_backtrace.should contain("Caused by")
ex.inspect_with_backtrace.should contain("inner")
end
end
6 changes: 6 additions & 0 deletions src/exception.cr
Expand Up @@ -59,6 +59,12 @@ class Exception
io.print " from "
io.puts frame
end

if cause = @cause
io << "Caused by: "
cause.inspect_with_backtrace(io)
end

io.flush
end
end
Expand Down

0 comments on commit 49d722c

Please sign in to comment.