err-handler

Crates.ioerr-handler
lib.rserr-handler
version1.0.0
sourcesrc
created_at2024-04-04 08:53:25.292618
updated_at2024-05-26 03:19:53.625485
descriptiona non-intrusive error handling marco
homepage
repositoryhttps://github.com/shunlingsmile/err-handler
max_upload_size
id1196049
size21,274
shunling (shunlingsmile)

documentation

https://docs.rs/err-handler

README

err-handler is a non-intrusive error handling marco that enhances your error handling

If you need to handle errors externally or consolidate them in a single location, err-handler is your logical choice

[dependencies]
err-handler = "0.1.1"

Examples

use thiserror::Error;
use err_handler::err_handler;
#[derive(Debug, Error)]
enum Err {
    #[error("err1")]
    Err1,
    #[error("err2")]
    Err2,
}
// The ` err-handler` marco attribute can be any function name but must have a matching return type.
#[err_handler(task_handler)]
fn task(_v: i32) -> Result<i32, Err> {
    Err(Err::Err1)
}
fn task_handler(e: Err) -> Result<i32, Err> {
    match e {
        Err::Err1 => Ok(100),
        _ => Err(e)
    }
}
// If a target function is asynchronous, then its error handling function must also be asynchronous.
#[err_handler(crate::async_task_handler)]
async fn async_task(_v: i32) -> Result<i32, Err> {
    Err(Err::Err1)
}
async fn async_task_handler(e: Err) -> Result<i32, Err> {
    match e {
        Err::Err1 => Ok(100),
        _ => Err(e)
    }
}
#[tokio::main]
async fn main() -> Result<(), Err> {
    assert_eq!(task(0)?, 100);
    assert_eq!(async_task(0).await?, 100);
    Ok(())
}
Commit count: 7

cargo fmt