Skip to content

Remove enforcal of printing exceptions as red #36015

@KristofferC

Description

@KristofferC

The code here

julia/base/errorshow.jl

Lines 87 to 95 in 0413ef0

function showerror(io::IO, ex, bt; backtrace=true)
try
with_output_color(get(io, :color, false) ? error_color() : :nothing, io) do io
showerror(io, ex)
end
finally
backtrace && show_backtrace(io, bt)
end
end

tries its best to ensure that the showerror code an exception implement gets printed in red. This can mess up custom coloring that the exception itself tries to do in showerror. For example, here is some code that defines an exception text using colors:

julia> struct CustomException end

julia> function Base.showerror(io::IO, ce::CustomException)
           printstyled(io, "This is in red\n"; color=:light_red)
           println(io, "This is in white")
       end

Trying with the normal two arg showerror and things look ok. But when the error would actually be shown in REPL, the second line would have "magically" also turned red, ruining the formatting.

bild

If an exception wants to print its text in red, it can just implement that with printstyled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error handlingHandling of exceptions by Julia or the user

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions