How do I filter backtraces to only include up to top level as in g()
julia> function f()
try
error()
catch err
print(stderr, sprint(showerror, err), sprint(Base.show_backtrace, catch_backtrace()))
end
end
f (generic function with 1 method)
julia> f()
Stacktrace:
[1] error()
@ Base ./error.jl:42
[2] f()
@ Main ./REPL[8]:3
[3] top-level scope
@ REPL[9]:1
[4] eval
@ ./boot.jl:369 [inlined]
[5] eval_user_input(ast::Any, backend::REPL.REPLBackend)
@ REPL ~/julia-master/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:139
[6] repl_backend_loop(backend::REPL.REPLBackend)
@ REPL ~/julia-master/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:200
[.....]
julia> g() = error()
g (generic function with 1 method)
julia> g()
ERROR:
Stacktrace:
[1] error()
@ Base ./error.jl:42
[2] g()
@ Main ./REPL[10]:1
[3] top-level scope
@ REPL[11]:1
Alternative question: How do I print a stacktrace formatted like the one from g
from a catch
block?
I believe https://stackoverflow.com/a/59690947/2442087 answers your alternative question
Thanks, I thought it tried the three-arg showerrror, guess not.
Ah, I did try that, but it does not filter the trace.
Oh, I think you're right
I'm not at the computer, but I think there is a function in Test
called scrub_backtrace
or smthg like that which does some backtrace filtering
Thanks, Base.scrub_repl_backtrace
it is.
Last updated: Nov 06 2024 at 04:40 UTC