anyhow-auto-context 1.0.0

Automatic context for anyhow errors based on scope and location
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 18.52 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • imbolc/anyhow-auto-context
    4 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • imbolc

anyhow-auto-context

License Crates.io Docs.rs

Automatic context for anyhow errors based on scope and location.

Usage

With Option

use anyhow_auto_context::auto_context;

fn main() -> anyhow::Result<()> {
    let expected_some = None;
    auto_context!(expected_some)
}
$ cargo run --example option
Error: expected_some in option::main at examples/option.rs:6:5

With Result

use anyhow::Result;
use anyhow_auto_context::auto_context;

fn main() -> Result<()> {
    auto_context!(foo())
}

fn foo() -> Result<()> {
    auto_context!(bar())
}

fn bar() -> Result<()> {
    anyhow::bail!("my error")
}
$ cargo run --example result
Error: foo() in result::main at examples/result.rs:6:5

Caused by:
    0: bar() in result::foo at examples/result.rs:10:5
    1: my error

With additional context

The optional context arguments follow format! semantics and are evaluated only on the error path.

use anyhow::Result;
use anyhow_auto_context::auto_context;

fn main() -> Result<()> {
    for i in 1..100 {
        auto_context!(not_42(i), "i = {i}")?;
    }
    Ok(())
}

fn not_42(n: usize) -> Result<()> {
    anyhow::ensure!(n != 42, "Unexpected 42");
    Ok(())
}
$ cargo run --example context
Error: not_42(i) in context::main at examples/context.rs:7:9 with i = 42

Caused by:
    Unexpected 42

Contributing

  • please run .pre-commit.sh before sending a PR, it will check everything

License

This project is licensed under the MIT license.