Crates.io | error-stack-derive |
lib.rs | error-stack-derive |
version | 0.1.0 |
source | src |
created_at | 2022-08-22 16:59:16.413246 |
updated_at | 2022-08-22 16:59:16.413246 |
description | A derive macro to use in pair with error_stack or generally any error system |
homepage | https://catuniversity.pages.dev/ |
repository | https://github.com/CatUniversity/error_stack_derive/ |
max_upload_size | |
id | 650479 |
size | 20,057 |
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())
}