Using anyhow
, I can add context to each error individually. Is there a way to set context for all errors returned from a function?
For instance, in the following case:
use anyhow::{anyhow, Result};
fn add(n: u16) -> Result<u16> {
if n < 10 {
Err(anyhow!("Smaller than 10").context("cant add"))
} else if n < 20 {
Err(anyhow!("Smaller than 20").context("cant add"))
} else {
Ok(n + 1)
}
}
fn main() {
let n = add(5);
println!("n: {}", n.unwrap());
}
is there a way to add context to every error at once without having to manually add it to every error?