rust-errors-thiserror-anyhow
rust-errors-thiserror-anyhow
Chooses and applies the correct error-handling crate. thiserror and anyhow are not competitors: they serve opposite sides of the API boundary. thiserror builds structured error types whose callers can match on variants and recover. anyhow builds opaque error values whose callers only display the message and the source chain. Picking the wrong one leaks an unmatchable error type into a library API or buries a binary in pointless boilerplate.
Cross-references: [[rust-impl-error-handling]] (the ? operator, Result vs panic!, custom error types from scratch), [[rust-errors-runtime]] (panics, unwrap/expect, Box<dyn Error>), [[rust-syntax-traits]] (the std::error::Error trait, From, derive macros), [[rust-impl-cargo-project]] (adding crate dependencies, library vs binary crates).
When to use this skill
- The user asks "which error crate should I use" or "thiserror vs anyhow".
- A library crate needs a public error type whose callers must match on causes.
- A binary or application crate needs
fn main() -> Result<()>and short error glue. - An error message no longer carries its underlying cause (broken source chain).
- The user wants to recover a concrete error type out of an opaque
anyhow::Error. - A project mixes both crates and the boundary between them is unclear.
- The user wants to migrate code from
anyhowtothiserroror the reverse.