errer_derive

Crates.ioerrer_derive
lib.rserrer_derive
version0.13.1
sourcesrc
created_at2019-06-18 22:40:58.593575
updated_at2019-06-29 20:09:34.738746
descriptionFlexible error management for Rust. An middle-ground between failure and SNAFU
homepage
repositoryhttps://github.com/engineeringvirtue/errer
max_upload_size
id142021
size27,284
dreamatic (Thomas-QM)

documentation

README

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.
Commit count: 0

cargo fmt