error-stack-derive

Crates.ioerror-stack-derive
lib.rserror-stack-derive
version0.1.0
sourcesrc
created_at2022-08-22 16:59:16.413246
updated_at2022-08-22 16:59:16.413246
descriptionA derive macro to use in pair with error_stack or generally any error system
homepagehttps://catuniversity.pages.dev/
repositoryhttps://github.com/CatUniversity/error_stack_derive/
max_upload_size
id650479
size20,057
Han Seung Min - 한승민 (arHSM)

documentation

README

Error Stack Derive

badge

A simple crate with a simple derive macro to make your error handling workflow simpler than ever :)

Check out the examples directory for, well, examples and check out the docs for more information about the macro.

Or, here's one

use std::fs::read_to_string;

use error_stack::{IntoReport, ResultExt};
use error_stack_derive::ErrorStack;

#[derive(ErrorStack, Debug)]
#[error_message(&format!("Error occured with foo ({}, {})", self.bar, self.baz))]
struct FooError {
    bar: u8,
    baz: u8,
}

fn main() {
    let foo = read_to_string("foo.txt")
        .report()
        .change_context(FooError { bar: 0, baz: 1 });

    assert!(foo.is_err());
    let err = foo.err().unwrap();
    // Error occured with foo (0, 1)
    //              at examples/structs.rs:16:10
    // 
    // Caused by:
    //    0: No such file or directory (os error 2)
    //              at examples/structs.rs:15:10
    // FooError { bar: 0, baz: 1 }
    println!("{:?}\n{:?}", err, err.downcast_ref::<FooError>().unwrap())
}
Commit count: 3

cargo fmt