anyhow::Context for adding error context without custom types

13011
0

When prototyping or writing applications (not libraries), anyhow is my go-to. It provides Result<T> as an alias for Result<T, anyhow::Error>, which can hold any error type. The .context() method attaches additional context to errors as they propagate, which is invaluable for debugging. You can chain context at each layer without defining custom error enums. This keeps code concise while still providing rich error messages. The downside is you lose compile-time error matching, so I only use it in binaries where error handling is mostly "log and exit." For libraries, I still prefer thiserror for typed errors. But for CLI tools and services where you want fast iteration and good diagnostics, anyhow is unbeatable.