diff --git a/src/taskcopy.jl b/src/taskcopy.jl index 3d2e693f..e30d803b 100644 --- a/src/taskcopy.jl +++ b/src/taskcopy.jl @@ -40,8 +40,8 @@ proper way is refreshing the `current_task` (the variable `t`) in function task_wrapper(func) () -> try - res = func() ct = current_task() + res = func() ct.result = res isa(ct.storage, Nothing) && (ct.storage = IdDict()) ct.storage[:_libtask_state] = :done @@ -83,6 +83,20 @@ function Base.copy(t::Task) newt end +struct CTaskException + etype + msg::String + backtrace::Vector{Union{Ptr{Nothing}, Base.InterpreterIP}} +end + +function Base.show(io::IO, exc::CTaskException) + println(io, "Stacktrace in the failed task:\n") + println(io, exc.msg * "\n") + for line in stacktrace(exc.backtrace) + println(io, string(line)) + end +end + produce(v) = begin ct = current_task() @@ -178,7 +192,12 @@ consume(p::Task, values...) = begin return p.result end if p.exception != nothing - throw(p.exception) + msg = if :msg in fieldnames(typeof(p.exception)) + p.exception.msg + else + string(typeof(p.exception)) + end + throw(CTaskException(typeof(p.exception), msg, p.backtrace)) end end wait() diff --git a/test/brokentask.jl b/test/brokentask.jl index 8546d141..aa9720b7 100644 --- a/test/brokentask.jl +++ b/test/brokentask.jl @@ -17,7 +17,7 @@ r = @testset "Broken Functions Tests" begin try consume(t) catch ex - @test isa(ex, ErrorException) + @test ex.etype == ErrorException end @test isa(t.exception, ErrorException) end @@ -37,7 +37,7 @@ r = @testset "Broken Functions Tests" begin try consume(t) catch ex - @test isa(ex, BoundsError) + @test ex.etype == BoundsError end @test isa(t.exception, BoundsError) end @@ -58,7 +58,7 @@ r = @testset "Broken Functions Tests" begin try consume(t) catch ex - @test isa(ex, BoundsError) + @test ex.etype == BoundsError end @test isa(t.exception, BoundsError) end @@ -80,7 +80,7 @@ r = @testset "Broken Functions Tests" begin try consume(t_copy) catch ex - @test isa(ex, BoundsError) + @test ex.etype == BoundsError end @test isa(t_copy.exception, BoundsError) end diff --git a/test/clonetask.jl b/test/clonetask.jl index e3c967c9..7315c093 100644 --- a/test/clonetask.jl +++ b/test/clonetask.jl @@ -56,4 +56,4 @@ function g_break() end t = CTask(g_break) -@test_throws MethodError consume(t) +@test_throws Libtask.CTaskException consume(t)