handle-error 0.1.2

An error handling helper macro to avoid the constant if let Err(e) pattern
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 1 items with examples
  • Size
  • Source code size: 6.21 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ryankurte/rust-handle-error
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ryankurte

handle-error

An error handling / bubbling macro to reduce rust error handling boilerplate where ? doesn't work because the site of the error matters.

GitHub tag Travis Build Status Crates.io Docs.rs

For a given fallible expression (expression returning a result), such as:

fn do_something() -> Result<(), E> {
    // ....
}

This can be used as follows:

#[macro_use]
extern crate log;

#[macro_use]
extern crate handle_error;

fn main() -> Result<(), E> {
  let v = handle_error!(do_something(), "Failed to do something");
  Ok(())
}

Replacing the common patterns:

#[macro_use]
extern crate log;

// Match case where we care about the ok value
fn example_one() -> Result<(), E> {
  let v = match do_something() {
    Ok(v) => v,
    Err(e) => {
      error!("Failed to do something");
      return Err(e);
    }
  };

  Ok(())
}

// If let where we do not care about the ok value
fn example_two() -> Result<(), E> {
  if let Err(e) = do_something() {
    error!("Failed to do something");
    return Err(e);
  }

  Ok(())
}