`thiserror` / `anyhow`
thiserror derives structured error enums for libraries. anyhow provides a boxed, context-carrying error type for applications. They solve different problems and often appear in the same workspace.
Detailed Explanation
thiserror is a derive macro. You write an enum of failure cases, attach #[error("file {0} missing")] display strings, and optionally #[from] so ? converts underlying errors into your variants. The output implements Display, Error, and From for the sources you marked. Callers can match on ConfigError::MissingPath and the compiler will notice a new variant. That is a library API.
anyhow is a type, anyhow::Error, plus the anyhow::Result<T> alias and helpers like anyhow!(...), bail!, and .context("reading config"). One type holds any Error plus a chain of context strings. You cannot usefully match on it. You print it, log it, or return it from main. That is an application API.
The usual split: a crate other people depend on uses thiserror (or a hand-written error enum). A binary or a service uses anyhow so main can ? through I/O, SQL, and HTTP without naming fifteen error types. The binary depends on the library, and ? converts the library error into anyhow::Error because anyhow implements From for every Error.
Context is the feature people adopt anyhow for. fs::read(&path).with_context(|| format!("read {}", path.display()))? keeps the io::Error as the source and adds the path. thiserror can do similar with #[source] and display templates, but you decide the variants up front. anyhow lets you add context at the call site without extending an enum.
Do not put anyhow::Error in a library's public signature unless the library is an application-shaped toolkit. Downstream crates cannot match it, and you cannot later make it structured without breaking them. Do not put a twenty-variant thiserror enum in a binary that never matches it; you will spend your time writing From impls that anyhow would have given you.
When you reach for thiserror: a published crate, or an internal crate whose callers match. When you reach for anyhow: main, handlers, jobs. When you reach for neither: a single io::Error is already enough.
Code Examples
Library-shaped error (thiserror would generate the Display/Error/From impls).
// Library-shaped error (thiserror would generate the Display/Error/From impls).
#[derive(Debug)]
enum ConfigError {
Io(std::io::Error),
}
impl From<std::io::Error> for ConfigError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
fn read_port(raw: &str) -> Result<u16, std::num::ParseIntError> {
raw.parse()
}
fn main() {
match read_port("8080") {
Ok(p) => println!("{p}"), // prints: 8080
Err(e) => println!("{e}"),
}
let _ = ConfigError::from(std::io::Error::other("demo"));
}Common Gotchas
`anyhow` in a library public API traps callers
They get a blob. Use `thiserror` or a manual enum for anything you publish.
`#[from]` is what makes `?` convert
Without it, `File::open(path)?` inside `-> Result<T, MyError>` will not compile even if `MyError` can wrap `io::Error` by hand.
Context strings are not variants
`.context("retry")` does not give you a `Retry` you can match. If the caller must branch, add a variant.
Both crates are opt-in dependencies
The language does not include them. A tiny crate can implement `Error` by hand in twenty lines.