error-trace-rs
Small Rust crate for printing nice errors traits based on std::error::Error::source().
Usage
Using the crate is quite straightforward.
First, create your errors as usual:
Then, when it is time to report them to the user, do not print them directly but instead use the ErrorTrace-trait's trace() function:
use ErrorTrace as _;
// ...
let err: HigherError = HigherError ;
eprintln!;
This will show you:
Oh no, something went wrong!
Caused by:
o A specific reason
If you enable the colors-feature, you can additionally print some neat colours:
use ErrorTrace as _;
// ...
let err: HigherError = HigherError ;
// Requires the `colors`-feature!
eprintln!;

Finally, when used in a situation where you want to show a quick error but are sure to never needs its contents, you can use the toplevel!()-macro:
use toplevel;
// Do something that fails
let err = from_utf8.unwrap_err;
// Format it with a one-time parent error
eprintln!;
For users of the colors-feature, there is the associated toplevel_colored!()-macro:
use toplevel_colored;
// Do something that fails
let err = from_utf8.unwrap_err;
// Format it with a one-time parent error
eprintln!;
Freezing
In some scenarios, an error you may have is not itself 'static and can therefore not be propagated in Error::source(). To workaround this issue, consider the ErrorTrace::freeze()-function: it returns a FrozenTrace-object, which returns the same trace as the original error would except that it has serialized all errors into Strings. As such, it is decoupled from the type.
See the documentation of ErrorTrace::freeze() or FrozenTrace for more information.
Installation
To use this crate into one of your projects, simply add it to your Cargo.toml file:
= { = "https://github.com/Lut99/error-trace-rs" }
Optionally, you can commit to a particular tag:
= { = "https://github.com/Lut99/error-trace-rs", = "v4.0.0" }
To build this crate's documentation and open it, run:
in the root of the repository.
Features
The crate has the following features:
colors: Enables the use of [trace_colored()].macros: Enables the use of the [toplevel!()]- and [toplevel_coloured!()]-macros.serde: ImplementsDeserializeandSerializefor theFrozenTrace-structure.
Contribution
If you are interested in contributing in this project, feel free to raise an issue or create a pull request. Note that this is mainly a hobby project, so not all suggestions may be accepted no matter how good it is ;)
License
This project is now licensed under the Apache 2.0 license. See LICENSE for more information.