Skip to content

Commit 4e8b209

Browse files
committed
thunk: Improve ThunkFailedException printing
1 parent 66d5147 commit 4e8b209

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

src/thunk.jl

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,59 @@ ThunkFailedException(thunk, origin, ex::E) where E =
193193
ThunkFailedException{E}(convert(WeakThunk, thunk), convert(WeakThunk, origin), ex)
194194
function Base.showerror(io::IO, ex::ThunkFailedException)
195195
t = unwrap_weak(ex.thunk)
196-
o = unwrap_weak(ex.origin)
197-
t_str = t !== nothing ? "$t" : "?"
198-
o_str = o !== nothing ? "$o" : "?"
196+
197+
# Find root-cause thunk
198+
last_tfex = ex
199+
failed_tasks = Union{Thunk,Nothing}[]
200+
while last_tfex.ex isa ThunkFailedException && unwrap_weak(last_tfex.ex.origin) !== nothing
201+
push!(failed_tasks, unwrap_weak(last_tfex.thunk))
202+
last_tfex = last_tfex.ex
203+
end
204+
o = unwrap_weak(last_tfex.origin)
205+
root_ex = last_tfex.ex
206+
207+
function thunk_string(t)
208+
if t === nothing
209+
return "Thunk(?)"
210+
end
211+
Tinputs = Any[]
212+
for input in t.inputs
213+
input = unwrap_weak(input)
214+
if istask(input)
215+
push!(Tinputs, "Thunk(id=$(input.id))")
216+
else
217+
push!(Tinputs, input)
218+
end
219+
end
220+
t_sig = if length(Tinputs) <= 4
221+
"$(t.f)($(join(Tinputs, ", "))))"
222+
else
223+
"$(t.f)($(length(Tinputs)) inputs...)"
224+
end
225+
return "Thunk(id=$(t.id), $t_sig"
226+
end
227+
t_str = thunk_string(t)
228+
o_str = thunk_string(o)
199229
t_id = t !== nothing ? t.id : '?'
200230
o_id = o !== nothing ? o.id : '?'
201-
println(io, "ThunkFailedException ($t failure",
202-
(o !== nothing && t != o) ? " due to a failure in $o)" : ")",
203-
":")
204-
Base.showerror(io, ex.ex)
231+
println(io, "ThunkFailedException:")
232+
println(io, " Root Exception Type: $(typeof(root_ex))")
233+
println(io, " Root Exception:")
234+
Base.showerror(io, root_ex); println(io)
235+
if t !== o
236+
println(io, " Root Thunk: $o_str")
237+
if length(failed_tasks) <= 4
238+
for i in failed_tasks
239+
i_str = thunk_string(i)
240+
println(io, " Inner Thunk: $i_str")
241+
end
242+
else
243+
println(io, " ...")
244+
println(io, " $(length(failed_tasks)) Inner Thunks...")
245+
println(io, " ...")
246+
end
247+
end
248+
print(io, " This Thunk: $t_str")
205249
end
206250

207251
"""

test/thunk.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ end
9595
end
9696
ex = Dagger.Sch.unwrap_nested_exception(ex)
9797
ex_str = sprint(io->Base.showerror(io,ex))
98-
@test occursin(r"^ThunkFailedException \(Thunk.*failure\):", ex_str)
98+
@test occursin(r"^ThunkFailedException:", ex_str)
9999
@test occursin("Test", ex_str)
100-
@test !occursin("due to a failure in", ex_str)
100+
@test !occursin("Root Thunk", ex_str)
101101

102102
ex = try
103103
fetch(b)
@@ -106,8 +106,8 @@ end
106106
end
107107
ex = Dagger.Sch.unwrap_nested_exception(ex)
108108
ex_str = sprint(io->Base.showerror(io,ex))
109-
@test occursin(r"Thunk.*failure due to a failure in", ex_str)
110109
@test occursin("Test", ex_str)
110+
@test occursin("Root Thunk", ex_str)
111111
end
112112
@testset "single dependent" begin
113113
a = @spawn error("Test")

0 commit comments

Comments
 (0)