#error #error-management #flexible #derive #snafu #context #errer

macro errer_derive

Flexible error management for Rust. An middle-ground between failure and SNAFU

6 releases

0.13.1 Jun 29, 2019
0.13.0 Jun 21, 2019
0.12.2 Jun 21, 2019
0.1.1 Jun 20, 2019

#7 in #snafu

Download history 28/week @ 2024-03-30 7/week @ 2024-04-06

287 downloads per month

MIT license

30KB
558 lines

errer - smooth & flexible error management

with ideas from failure and SNAFU.

Rundown

use errer::ErrorContext;

#[derive(Errer)]
enum Error {
    #[errer(from)] //implements From<request::Error>
    Request(request::Error),
    //implements Display
    //from specifies the base for ErrorContext, which is implemented with context
    //this generates a struct, akin to SNAFU, except you can use context on structs and enums of all kinds
    #[errer(from, context, display = "Error parsing section {}: {}", 1, 0)]
    ParsingSection { from: parse::Error, section: usize },
    #[errer(from, std, display = "IO error: {}", 0)] //std implements std::error::Error if Self: Debug + Display
    IO(io::Error)
}

res!(Error);

fn main_res() -> Res<()> {
    let data = request::do()?;
    data.parse().context(ParsingSection { section: 0 })?;

    Ok(())
}

main_res!(main_res);

A few tips

  • Context is dependent on from to specify the base error; take a peek at the internal traits for more information.
  • You can use all the attributes on structs and the outside of enums (to set defaults) as well.
  • You can use #[errer(context = name)] to set the name of the generated context struct.
  • Similarly, you can use #[errer(from = member)] to set the member that from uses.
  • You can use display on the outside of enums to set a surrounding format string.
  • And remember that you can always implement Display yourself.

Dependencies

~2MB
~46K SLoC