invenia / Memento.jl

A flexible logging library for Julia
https://invenia.github.io/Memento.jl/latest
Other
87 stars 14 forks source link

Include both message and caught error #126

Closed nickrobinson251 closed 4 years ago

nickrobinson251 commented 5 years ago

Currently I write something like this (or some other workaround)

try
    ...
catch err
    err isa ArgumentError || rethrow(err)
    warn(logger, "message...")
    error(logger, err)
end

But perhaps it should be possible to write this?

error(logger, "message...", err)
rofinn commented 4 years ago

I think the typical pattern for this is to wrap the exception in another exception

nickrobinson251 commented 4 years ago

I am glad there is another way, but I do not know what that means, please can you give an example?

rofinn commented 4 years ago
try
    ...
catch err
    err isa ArgumentError || rethrow(err)
    error(logger, ErrorException("message ... " * sprint(io -> showerror(io, err)))
end
rofinn commented 4 years ago

If it is a common enough pattern then typically a new container exception type is defined (e.g.,HTTP.IOError, CaptureException). NOTE: There has been some discussion that error shouldn't actually throw an error, but rather just log it and requiring a separate throw.

rofinn commented 4 years ago

Unless I'm mistaken there isn't anything here that Memento needs to fix?

nickrobinson251 commented 4 years ago

error(logger::Logger, msg::AbstractString, e::Excpetion) = error(logger, ErrorException(msg * sprint(io -> showerror(io, e)))?