derive(Error)

github crates.io docs.rs build status

This library provides a convenient derive macro for the standard library's [std::error::Error] trait.

toml [dependencies] thiserror = "1.0"

Compiler support: requires rustc 1.31+


Example

```rust use thiserror::Error;

[derive(Error, Debug)]

pub enum DataStoreError { #[error("data store disconnected")] Disconnect(#[from] io::Error), #[error("the data for key {0} is not available")] Redaction(String), #[error("invalid header (expected {expected:?}, found {found:?})")] InvalidHeader { expected: String, found: String, }, #[error("unknown data store error")] Unknown, } ```


Details


Comparison to anyhow

Use thiserror if you care about designing your own dedicated error type(s) so that the caller receives exactly the information that you choose in the event of failure. This most often applies to library-like code. Use [Anyhow] if you don't care what error type your functions return, you just want it to be easy. This is common in application-like code.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.