5 releases

0.2.3 Mar 4, 2023
0.2.2 Mar 4, 2023
0.2.1 Mar 4, 2023
0.2.0 Mar 4, 2023
0.1.0 Mar 4, 2023

#1758 in Rust patterns

ISC license

8KB
121 lines

error_hook

This library supplements errors on a per-function basis and executes hooks.

Example

use error_hook::{self, hook};

#[hook(e => tracing::error!("{e}"))]
#[tracing::instrument]
async fn test(a: i32, b: i32) -> error_hook::Result<i32> {
    a.checked_mul(b).ok_or(anyhow::anyhow!("overflow"))
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .init();

    let ans = test(888888888, 888888888).await?;
    println!("{ans}");

    Ok(())
}

output

2023-03-04T06:23:33.048087Z ERROR test{a=888888888 b=888888888}: example: overflow

lib.rs:

error_hook is a library for automation to insert action at error conversion.

Example

use error_hook_attr::hook;

#[hook(e => println!("{e}"))]
fn test(a: i32, b: i32) -> error_hook::Result<i32> {
    a.checked_mul(b).ok_or(anyhow::anyhow!("overflow"))
}

fn main() -> anyhow::Result<()> {
    let _ = test(888888888, 888888888);
    //      ^^^^ this prints 'overflow'

    Ok(())
}

Dependencies

~2.9–4.5MB
~92K SLoC