Rust Error Handling
Written
— Updated
error_stack- This is my current library of choice for error handling.
- Not a full replacement for
thiserror, but a complement to it. - Allows attaching messages to arbitrary errors. No more
std::io::Errorwithout the filename. - Can collecting multiple errors, useful when running concurrent tasks.
- Preserves the full stack of errors as they progress between modulgje boundaries, so you can see what actually happened, nicely pretty-printed.
- To remove printing file locations in error stack, for friendlier user-facing errors:
;
eyreis a fork ofanyhowfocused on better error reporting- Snafu
- This is a comprehensive library with a lot of options. The big advantage here is that a lot of care is taken to make rich context information easy to use.
- This generates an
ErrorTypestructure that can be used to add content to anAnErrorerror.ErrorTypecontains everything except the source and backtrace fields. This can then be used with the context trait.- The context type also uses generics parameterized with
Into<OriginalType>, so you don't have to do things liketo_string()all over the place.
- The context type also uses generics parameterized with
do_it_or_anerror().context(ErrorType{ operation: "doing it" })- Source fields can be mapped using
#[snafu(source(from(ErrorType, MapFunc)))] - Applications may want to enable the
backtraces-impl-backtrace-cratefeature to capture backtraces. Theunstable-backtraces-impl-stdfeature does the same for Rust's built-in backtraces, but those aren't stabilized yet. - The
futuresfeature adds context methods to futures and streams that return Results.