Stacked Errors
A crate for high level error propogation with software controlled backtraces
that are entirely independent of the RUST_BACKTRACE
system.
In Rust development, major crates will often have their own error enums that
work well in their own specialized domain, but when orchestrating many
domains together we run into issues. map_err
is very annoying to work
with. In async
call stacks or cases where we can't easily control
RUST_BACKTRACE
, we run into an especially annoying problem
where the same kind of error can be returned from multiple places, and we
are sometimes forced into println
debugging to find out where it is
actually from. This crate introduces the StackableErr
trait and a
"stackable" error type that allows for both software-defined error
backtraces and easily converting errors into the stackable error type.
This crate is similar to eyre
, but has a more efficient internal layout
with a ThinVec
array of SmallBox
es, works with no_std
, implements
core::error::Error
, and more.
Some partial examples of what using the crate looks like:
f.map_err?;
// replace the above with
f.stack?; // uses `#[track_caller]` when an error is being propagated
let dir = self
.path
.parent
.stack_err?
.to_str
.stack_err?;
// arbitrary things implementing `Display + Send + Sync + 'static` can be stacked
f.stack_err?;
// readily swappable with `anyhow` and `eyre` due to extra method, trait, and
// struct aliases
f.wrap_err?;
f.context?;
// The trait is implemented for options so that we don't need `OptionExt` like
// `eyre` does
option.take
.stack_err?
.wait_with_output
.await
.stack_err_with?;
return Err
// replace the above with
bail!
// when the error type is already `stacked_errors::Error` you can do this if it is
// preferable over `map`
return match ...
use ;
// Note that `Error` uses `ThinVec` internally, which means that it often
// takes up only the stack space of a `usize` or the size of the `T` plus
// a byte.
let res = format!;
assert_eq!;
// The line numbers are slightly off because this is a doc test.
// In order from outer to the innermost call, it lists the location of the
// `stack` call from `outer`, the location of `stack_err` from `inner`,
// the associated error message, the location of either the `Error::from`
// or `stack_err` from `innermost`, and finally the root error message.
// note that debug mode (used when returning errors from the main function)
// includes terminal styling
println!;
let res = format!;
assert_eq!;
println!;
let res = format!;
assert_eq!;
// in commonly used functions you may want `_locationless` to avoid adding
// on unnecessary information if the location is already being added on
return Err.stack_err_with