errer

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

4 releases (2 breaking)

0.13.0 Jun 21, 2019
0.12.1 Jun 20, 2019
0.1.1 Jun 20, 2019
0.1.0 Jun 18, 2019

#1505 in Rust patterns


Used in errer_derive

MIT license

5KB
70 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

~0–6.5MB
~38K SLoC