The errors ecosystem
Good crates
The following crates are commonly used and recommended.
ThisError
A powerful derive(Error)
macro. Implements Error
and Display
for your error types, including convenient syntax for source errors, backtraces, and conversions.
Anyhow
A crate for working with trait object error types. Extends the features of the standard library's Error
trait. However, most of Anyhow's features have been added to std, so you might not need Anyhow any more (it does have the advantage that it doesn't require unstable features, so can be used with a stable toolchain).
Snafu
Supports deriving error handling functionality for error types (including the single struct style, not just enum style errors), macros for throwing errors, and using strings as errors.
Error Stack
An alternative and extension to this-error, error-stack helps define your error types, but also adds support for better stacks of errors with arbitrary added attachments and information about how the error is produced and propagated. It also has sophisticated error reporting and formatting.
Error reporting crates
These crates are for reporting errors to the user in pretty ways. They are particularly useful for reporting errors in input text (e.g., source code) and are somewhat inspired by rustc's error reporting style.
Eyre is a fork of Anyhow with enhanced reporting facilities. The following crates are just for reporting and work with other error libraries if required:
Historic crates
As Rust's error handling story has evolved, many error handling libraries have come and gone. The following were influential in their time, but there are now better alternatives (sometimes including just the support in std). You might still see these crates used in historic documentation, but we wouldn't recommend using them any more.
- error-chain (support is now in std)
- failure (support is now in std)
- fehler blog post (an experiment which is no longer maintained)
- err-derive (support is built in to the recommended crates above)