A Rust library for error handling when you want your functions to automatically generate errors.
- Define error types directly in function expressions.
- Generate error types binding to the function.
Add the following to your Cargo.toml:
cargo add fnerrorYou also need to add thiserror to your crate: (Will be unneeded in the future)
cargo add thiserror#[fnerror]
fn foo() -> Result<(), MyError> /* or Result<T> to use the default ident of error type ,
which is pascal case of the function name + "Error", like `FooError` */
{
bar().map_err(|e| {
#[fnerr]
Error2("{}", e as String)
})?;
baz().map_err(|e| {
#[fnerr]
Error3("{}, {}", e as &'static str, 123 as u8)
})?;
Ok(())
}
fn bar() -> Result<(), String> {
Err("test2 error".to_string())
}
fn baz() -> Result<(), &'static str> {
Err("test2 error")
}Which expands to (with thiserror feature):
fn foo() -> ::std::result::Result<(), MyError> {
bar().map_err(|e| MyError::Error2(e))?;
baz().map_err(|e| MyError::Error3(e, 123))?;
Ok(())
}
#[derive(Debug, ::thiserror::Error)]
pub enum MyError {
#[error("{}", 0usize)]
Error2(String),
#[error("{}, {}",0usize, 1usize)]
Error3(&'static str, u8),
}- Parse AST to generate error types.
- Generate error implementations using thiserror crate.
- Support generic error types (experimental).
- Support custom error name.
- Replace panics with errors.
- Support other error implementations.
- Support more formatting options.