Skip to content

Commit

Permalink
Add support for Base.display_error over ExceptionStack.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Sep 23, 2021
1 parent 2f4df48 commit cc952cc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.38.0"
version = "3.39.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
11 changes: 11 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,17 @@ if VERSION < v"1.7.0-DEV.1106"
end
end

if VERSION >= v"1.2"
# Base.scrub_repl_backtrace only exists as of Julia 1.2,
# hence the immediately preceding version guard.
function Base.display_error(io::IO, stack::ExceptionStack)
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
bt = Any[ (x[1], Base.scrub_repl_backtrace(x[2])) for x in stack ]
show_exception_stack(IOContext(io, :limit => true), bt)
println(io)
end
end

function Base.show(io::IO, ::MIME"text/plain", stack::ExceptionStack)
nexc = length(stack)
printstyled(io, nexc, "-element ExceptionStack", nexc == 0 ? "" : ":\n")
Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,27 @@ end
caused by: UndefVarError: __not_a_binding__ not defined"""s,
sprint(show, excs_sans_bts))

if VERSION >= v"1.2"
# The tested implementation of Base.display_error is only available
# as of Julia 1.2, hence the immediately preceding version guard.

# Check that the ExceptionStack with backtraces `display_error`s correctly:
@test occursin(r"""
ERROR: DivideError: integer division error
Stacktrace:.*
caused by: UndefVarError: __not_a_binding__ not defined
Stacktrace:.*
"""s, sprint(Base.display_error, excs_with_bts))

# Check that the ExceptionStack without backtraces `display_error`s correctly:
@test occursin(r"""
ERROR: DivideError: integer division error
caused by: UndefVarError: __not_a_binding__ not defined"""s,
sprint(Base.display_error, excs_sans_bts))
end
else
# Due to runtime limitations, julia-1.0 only retains the last exception.

Expand Down

0 comments on commit cc952cc

Please sign in to comment.