derive(Error) for Rust's std::error::Error trait
open sourceMIT OR Apache-2.0free
thiserror provides a derive macro for the standard library's std::error::Error trait in Rust. Put #[derive(Error)] on an enum or struct and #[error("...")] on each variant to generate its Display message, #[from] to generate a From conversion, and #[source] (or a field named source) to report the underlying error; error(transparent) forwards an inner error's message and source. The generated code is what you'd write by hand, so thiserror doesn't appear in your public API, and switching to or from handwritten impls isn't a breaking change. Its README suggests it for code that designs its own error types, most often libraries, and anyhow, by the same author, for application code that just wants errors to be easy.
Problems it solves
- define custom error types in my Rust library without boilerplate
- convert an io::Error into my own error enum automatically
- give each variant of my Rust error enum a readable message
Features
- derive macro for std::error::Error
- Display messages from #[error] attributes
- From impls from #[from]
- source() from #[source] fields
- error(transparent) forwarding
- backtrace support
Alternatives
The same job
These do what thiserror does.
- anyhow A flexible concrete error type for easy error handling in Rust applications