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::{Context, Result};
fn get_cluster_info() -> Result<ClusterMap> {
let config = std::fs::read_to_string("cluster.json")
.context("Failed to read cluster config")?;
let map: ClusterMap = serde_json::from_str(&config)
.context("Failed to parse cluster config")?;
Ok(map)
}
#[derive(serde::Deserialize)]
struct ClusterMap {
// ..
}
Macros§
- anyhow
- Construct an ad-hoc error from a string or existing non-
anyhow
error 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-
anyhow
error value.
Structs§
- Chain
- Iterator of a chain of source errors.
- Error
- The
Error
type, a wrapper around a dynamic error type.
Traits§
- Context
- Provides the
context
method forResult
.
Functions§
- Ok
- Equivalent to
Ok::<_, anyhow::Error>(value)
.
Type Aliases§
- Result
Result<T, Error>