-
-
Save akzhan/13771b16dd12c01496a42a7153bcf72a to your computer and use it in GitHub Desktop.
raise with cause callstack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def raise(ex : Exception) : NoReturn | |
if ex.callstack.nil? | |
ex.callstack = CallStack.new | |
end | |
unwind_ex = Pointer(LibUnwind::Exception).malloc | |
unwind_ex.value.exception_class = LibC::SizeT.zero | |
unwind_ex.value.exception_cleanup = LibC::SizeT.zero | |
unwind_ex.value.exception_object = ex.object_id | |
unwind_ex.value.exception_type_id = ex.crystal_type_id | |
__crystal_raise(unwind_ex) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec" | |
private class SomeError < Exception | |
def initialize(cause) | |
super("Some error", cause) | |
end | |
end | |
describe "raise" do | |
ex = Exception.new "whatever" | |
callstack = CallStack.new | |
ex.callstack = callstack | |
it "should raise ex with callstack" do | |
expect_raises Exception, ex.backtrace do | |
raise ex | |
end | |
end | |
it "should raise SomeError with ex cause" do | |
expect_raises SomeError, ex.backtrace do | |
raise SomeError.new(ex) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment