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

Print exception cause when inspecting with backtrace #5833

Merged
merged 1 commit into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/std/exception_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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