#error-handling #lang #error #failure #rundown

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

#892 in Rust patterns

Download history 34/week @ 2022-05-30 37/week @ 2022-06-06 11/week @ 2022-06-13 14/week @ 2022-06-20 11/week @ 2022-06-27 36/week @ 2022-07-04 43/week @ 2022-07-11 9/week @ 2022-07-18 37/week @ 2022-07-25 34/week @ 2022-08-01 18/week @ 2022-08-08 19/week @ 2022-08-15 16/week @ 2022-08-22 15/week @ 2022-08-29 41/week @ 2022-09-05 13/week @ 2022-09-12

87 downloads per month
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

~40KB