talira

A flexible concrete error type for easy error handling in Rust applications

open sourceMIT OR Apache-2.0free

anyhow provides anyhow::Error, a trait object based error type for easy, idiomatic error handling in Rust applications. Return anyhow::Result<T> from any fallible function and use ? to propagate any error that implements std::error::Error, including your own crate's. context and with_context attach a message saying what higher-level step failed, errors can be downcast back to their concrete type, and on Rust 1.65 or later a backtrace is captured when the standard RUST_BACKTRACE or RUST_LIB_BACKTRACE environment variables enable it. The anyhow! macro builds a one-off error with string interpolation, and bail! returns one early. It works in no_std with a global allocator when the default std feature is turned off. Its README suggests it for application code that doesn't care which error type functions return, and thiserror, by the same author, for libraries that design their own error types.

Problems it solves

  • handle errors in my Rust application without defining an error enum
  • add context to a Rust error so I know which step failed
  • return any kind of error from a Rust function with the ? operator

Features

  • anyhow::Error and anyhow::Result
  • ? propagation of any std::error::Error
  • context and with_context messages
  • downcasting to the concrete error type
  • backtrace capture
  • anyhow! and bail! macros
  • no_std support

Alternatives

The same job

These do what anyhow does.

  1. thiserror derive(Error) for Rust's std::error::Error trait

Compare anyhow with the top 3