Crate okerr

Crate okerr 

Source
Expand description

§Ergonomic result / error handling helpers built on anyhow and thiserror.

Related docs:

This crate is a re-export of anyhow and aliase ofthiserror.

  • It provides a Result type that is anyhow::Result and okerr::derive::Error is an alias of thiserror::Error.
    • NOTE: okerr::derive::Error requieres the thiserror dependency to be added to your Cargo.toml (cargo add thiserror).
  • It also provides a err! macro that is a shorthand for Err(anyhow::anyhow!(...)) or Err(okerr::anyerr!(...)).
  • It also provides a fail! macro that is anyhow::bail!.
  • It also provides a anyerr! macro that is anyhow::anyhow!.
  • All “anyhow” is also available as “okerr” aliases (Context, ensure!, format_err!, etc.).

§Example

use okerr::{Result, err, fail, anyerr};

fn divide(a: i32, b: i32) -> Result<i32> {
    if b == 0 {
        err!("Cannot divide by zero")
    } else {
        Ok(a / b)
    }
}

fn maybe_fail(should_fail: bool) -> Result<String> {
  if should_fail {
    fail!("Oops!");
  }

  Ok("No error".to_string())
}

fn main() {
    let result = divide(10, 2);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 5);

    let result = divide(10, 0);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "Cannot divide by zero");

    // fail! does early return
    let result = maybe_fail(true);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "Oops!");

    // No error
    let result = maybe_fail(false);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "No error");

    // Same as anyhow!(...).
    // Creates an Error directly,
    // from a string or any std::error::Error
    let error = anyerr!("Oops!");
    assert_eq!(error.to_string(), "Oops!");
}

§Example with okerr::derive::Error

use okerr::{Result, err, derive::Error};

#[derive(Error, Debug)]
enum MyError {
    #[error("Cannot divide by zero")]
    DivideByZero,
    #[error("Cannot divide by {0}")]
    DivideBy(i32),
}

fn divide(a: i32, b: i32) -> Result<i32> {
    if b == 0 {
        err!(MyError::DivideByZero)
    } else if b < 0 {
        err!(MyError::DivideBy(b))
    } else {
        Ok(a / b)
    }
}

fn main() {
    let result = divide(10, 2);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 5);

    let result = divide(10, 0);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "Cannot divide by zero");

    let result = divide(10, -2);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "Cannot divide by -2");
}

It’s very simple, very lightweight (just a few lines of code in the okerr crate), no overhead, no abstraction cost. okerr provides consistency and a excellent DX. 100% compatible with anyhow and thiserror, convert easily error from a boxed error (like eyre::Report and others).

Modules§

derive
Sugar for thiserror::Error. okerr::derive::Error is an alias of thiserror::Error.

Macros§

anyerr
Same as anyhow! (and its alias: format_err!).
anyhow
Construct an ad-hoc error from a string or existing non-anyhow error value.
bail
Return early with an error.
ensure
Return early with an error if a condition is not satisfied.
err
Shorthand for Err(anyerr!(...)) or Err(anyhow!(...)).
fail
Same as anyhow::bail!.
format_err
Construct an ad-hoc error from a string or existing non-anyhow error value.

Structs§

Chain
Iterator of a chain of source errors.
Error
The Error type, a wrapper around a dynamic error type.

Traits§

Context
Provides the context method for Result.

Functions§

Ok
Equivalent to Ok::<_, anyhow::Error>(value).
from_boxed_error
Convert a boxed error into an okerr/anyhow Error.
wrap_err
Wrap a Result into an okerr/anyhow Error.

Type Aliases§

Result
Result<T, Error>