Skip to content

Instantly share code, notes, and snippets.

@akzhan

akzhan/raise.cr Secret

Created May 29, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akzhan/13771b16dd12c01496a42a7153bcf72a to your computer and use it in GitHub Desktop.
Save akzhan/13771b16dd12c01496a42a7153bcf72a to your computer and use it in GitHub Desktop.
raise with cause callstack
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
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