#error-derive #error #derive #debugging #error-stack

macro error-stack-derive

A derive macro to use in pair with error_stack or generally any error system

1 unstable release

0.1.0 Aug 22, 2022

#18 in #error-derive

25 downloads per month
Used in shanimation-rs

MIT license

14KB
133 lines

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())
}

Dependencies

~1.5MB
~34K SLoC