#error-context #context #fn-error-context

macro fn-error-context

An attribute macro to add context to errors from a function

5 releases

0.2.1 Mar 21, 2023
0.2.0 Jul 7, 2021
0.1.2 Apr 21, 2021
0.1.1 Jul 11, 2020
0.1.0 Mar 29, 2020

#434 in Procedural macros

Download history 4723/week @ 2024-12-15 1925/week @ 2024-12-22 2454/week @ 2024-12-29 3903/week @ 2025-01-05 4389/week @ 2025-01-12 5763/week @ 2025-01-19 6136/week @ 2025-01-26 6546/week @ 2025-02-02 5688/week @ 2025-02-09 5707/week @ 2025-02-16 6607/week @ 2025-02-23 4770/week @ 2025-03-02 5493/week @ 2025-03-09 6245/week @ 2025-03-16 5788/week @ 2025-03-23 5337/week @ 2025-03-30

23,291 downloads per month
Used in 15 crates (11 directly)

MIT/Apache

14KB
55 lines

This crate provides the context macro for adding extra error information to a function.

Works with anyhow, failure and any other error type which provides a context method taking a string.

#
use fn_error_context::context;

#[context("failed to parse config at `{}`", path.as_ref().display())]
pub fn parse_config(path: impl AsRef<Path>) -> anyhow::Result<u32> {
    let text = read_to_string(path.as_ref())?;
    Ok(text.parse()?)
}

let error = parse_config("not-found").unwrap_err();
assert_eq!(
    error.to_string(),
    "failed to parse config at `not-found`",
);
assert_eq!(
    error.source().unwrap().downcast_ref::<io::Error>().unwrap().kind(),
    io::ErrorKind::NotFound,
);

Crates.io Docs.rs

fn-error-context

An attribute macro to add context to errors from a function.

#[context("failed to parse config at `{}`", path.display())]
pub fn parse_config(path: &Path) -> anyhow::Result<u32> {
    let text = read_to_string(path)?;
    Ok(text.parse()?)
}

Dependencies

~195–620KB
~15K SLoC