3 unstable releases

0.2.1 Dec 17, 2022
0.2.0 Jul 9, 2022
0.1.0 Jul 7, 2022

#2002 in Rust patterns

Download history 10/week @ 2024-07-20 28/week @ 2024-07-27 28/week @ 2024-08-03 23/week @ 2024-08-10 12/week @ 2024-08-17 37/week @ 2024-08-24 40/week @ 2024-08-31 32/week @ 2024-09-07 40/week @ 2024-09-14 40/week @ 2024-09-21 20/week @ 2024-09-28 34/week @ 2024-10-05 46/week @ 2024-10-12 45/week @ 2024-10-19 52/week @ 2024-10-26 51/week @ 2024-11-02

195 downloads per month
Used in switchboard-on-demand-cli…

MIT license

7KB
127 lines

anyhow_ext

An extension of anyhow. A drop-in replacement of anyhow.

Features

  • Attach file location info in error message
  • Use dot to make a small backtrace.

usage

Work with anyhow!

[dependencies]
anyhow = "1"
anyhow_ext = "0.2"

Then use anyhow_ext::Context instead of anyhow::Context.

use anyhow::{anyhow, Result};
use anyhow_ext::Context;

fn foo() -> Result<()> {
    Err(anyhow!("an anyhow err")).context("wrap with file info")?;
    Ok(())
}

drop-in replacement of anyhow

Since anyhow_ext re-exports all the thing in anyhow except for Context, you can use anyhow_ext as a drop-in replacement.

Cargo.toml

[dependencies]
anyhow_ext = "0.2"

Then

use anyhow_ext::{Result,anyhow,Context};

fn foo() -> Result<()> {
    Err(anyhow!("an anyhow err")).context("wrap with file info")?;
    Ok(())
}

How location info is displayed

println!("{}", err) looks like

foo err at `src/bin/anyhow_ext.rs@6:11`

and println!("{:?}", err) looks like

  foo err at `src/bin/anyhow_ext.rs@6:11`

  Caused by:
  0: read_a_file err at `src/bin/anyhow_ext.rs@10:19`
  1: an io err at `src/bin/anyhow_ext.rs@15:55`
  2: No such file or directory (os error 2)

dot to make a small backtrace

use anyhow_ext::{Context, Result};

fn foo() -> Result<()> {
    std::fs::read_to_string("file_not_exists.txt").dot()?;
    Ok(())
}

fn bar() -> Result<()> {
    foo().dot()?;
    Ok(())
}

fn main() {
    if let Err(err) = try_main().dot() {
        println!("{:?}", err);
    }
}

fn try_main() -> Result<()> {
    bar().dot()?;
    Ok(())
}

This will make a small backtrace.

at `examples/dot.rs@14:34`

Caused by:
    0: at `examples/dot.rs@20:11`
    1: at `examples/dot.rs@9:11`
    2: at `examples/dot.rs@4:52`
    3: No such file or directory (os error 2)

Dependencies

~135KB