This project exists to allow one to easily configure their crate error types to conditionally collect diagnostic information. Also to handle things that one has to implement on an error (like Display).
use error_kind::{Err, Error, ErrorKind};
#[derive(Copy, Clone, Debug, ErrorKind)]
pub enum MyErrorKind {
SomeFailure,
SomeOtherFailure,
// ...
}
#[derive(Clone, Debug, Error)]
pub struct MyError {
error_kind: MyErrorKind,
}
fn some_function(case: u32) -> Result<(), MyError> {
match case {
1 => Err!(MyErrorKind::SomeFailure, "dynamic details: {case}"),
2 => Err!(MyErrorKind::SomeOtherFailure),
_ => Ok(()),
}
}
// This will print:
// error: some failure
println!("error: {}", some_function(1).unwrap_err());
For more information, check either the crates page, or read the project documentation.
I have a crate that I’m working on that I want to return typed errors within. However, I also want to be able to conditionally configure the crate to collect additional diagnostics information.
Usually, this detailed information is not useful for an end-user. So we would like for it to be compiled out in normal production usage. It’s only supposed to be used when testing and running debug versions of crates.
This is an alternate take on thiserror, where we enforce that Error is a different type from ErrorKind. That way, we can dress up the error value with additional information.
It turns out, some of these structures also allow for more convenient error handling. For example, you can use ErrorDomain types to easily convert between error types.