#context #auto #nightly #auto-add

macro auto-context

Auto-add context to anyhow errors (without nightly)

2 releases

0.1.1 Feb 2, 2025
0.1.0 Feb 2, 2025

#346 in Procedural macros

Download history 225/week @ 2025-01-31 26/week @ 2025-02-07

251 downloads per month

CC0 license

7KB
51 lines

auto-context

Auto-add context to anyhow errors (without nightly).

Example

use anyhow::{Context, Result};

struct Test {}

#[auto_context::auto_context]
impl Test {
  fn some_method(self) -> Result<()> {
    anyhow::bail!("some error")
  }

  fn some_function(_: i32) -> Result<()> {
    Test {}.some_method()?;

    Ok(())
  }
}

#[auto_context::auto_context]
fn main() -> Result<()> {
  Test::some_function(123)?;

  Ok(())
}

fails with

Error: Test::some_function(..) @ auto-context/src/lib.rs::23

Caused by:
    0: .some_method() @ auto-context/src/lib.rs::15
    1: some error

Details

The auto_context proc macro can be used to annotate any item. This includes functions, methods, and struct/trait impls.

Context is added to every [try expression] (every use of a ?). Different kinds of expressions result in different context formats:

  • method calls: .method_name(args)
  • function calls: some::func(args)
  • identifiers: xyz
  • expression calls: (.. some expr ..)

where args is .. if arguments are present and is empty otherwise.

Dependencies

~210–650KB
~15K SLoC