Expand description
Flexible error handling.
anyhow provides Error,
a trait object based error type for use cases
where you want error handling to be easy.
It is oftain said that Rust
libraries should define specific error types that implement std::error::Error,
so their callers can respond to errors precisely;
but applications can often just propagate errors up the call stack
without caring about specific error types.
For these more casual error handling use cases,
it is sometimes ideomatic in low-dependency Rust code
to return Box<dyn std::error::Error> as a generic
error type. anyhow provides a similar generic
error but with more features:
anyhow::Error requires no allocation in the success case,
supports downcasting to the original error type,
and can carry a backtrace, among others.
The Result<T> type alias saves typing,
and the Context trait provides the .context() method
for adding contextual information to errors as they propagate,
the anyhow! and bail! macros
For defining precise errors,
use thiserror instead of anyhow.
§Examples
Use Result<T> as your main error type,
and .context() to add helpful error messages:
use anyhow::{anyhow, bail, Context, Result};
fn load_config() -> Result<Config> {
let content = read_config_file()
.context("Unable to load config file")?;
let config = parse_config(&content)
.context("Invalid configuration format")?;
Ok(config)
}
struct Config {
name: String,
}
fn read_config_file() -> Result<String> {
Ok(std::fs::read_to_string("config.toml")?)
}
fn parse_config(content: &str) -> Result<Config> {
Ok(Config { name: content.to_string() })
}Macros§
- anyhow
- Construct an ad-hoc error from a string or existing non-
anyhowerror value. - bail
- Return early with an error.
- ensure
- Return early with an error if a condition is not satisfied.
- format_
err - Construct an ad-hoc error from a string or existing non-
anyhowerror value.
Structs§
- Chain
- Iterator of a chain of source errors.
- Error
- The
Errortype, a wrapper around a dynamic error type.
Traits§
- Context
- Provides the
contextmethod forResult.
Functions§
- Ok
- Equivalent to
Ok::<_, anyhow::Error>(value).
Type Aliases§
- Result
Result<T, Error>