Stream: helpdesk (published)

Topic: filtering backtraces


view this post on Zulip Fredrik Ekre (Jan 30 2021 at 00:23):

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

view this post on Zulip Fredrik Ekre (Jan 30 2021 at 00:25):

Alternative question: How do I print a stacktrace formatted like the one from g from a catch block?

view this post on Zulip Mosè Giordano (Jan 30 2021 at 00:27):

I believe https://stackoverflow.com/a/59690947/2442087 answers your alternative question

view this post on Zulip Fredrik Ekre (Jan 30 2021 at 00:30):

Thanks, I thought it tried the three-arg showerrror, guess not.

view this post on Zulip Fredrik Ekre (Jan 30 2021 at 00:32):

Ah, I did try that, but it does not filter the trace.

view this post on Zulip Mosè Giordano (Jan 30 2021 at 00:36):

Oh, I think you're right

view this post on Zulip Rafael Fourquet (Jan 30 2021 at 07:50):

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

view this post on Zulip Fredrik Ekre (Jan 30 2021 at 11:33):

Thanks, Base.scrub_repl_backtrace it is.


Last updated: Dec 28 2024 at 04:38 UTC