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
Resulttype that isanyhow::Resultandokerr::derive::Erroris an alias ofthiserror::Error.- NOTE:
okerr::derive::Errorrequieres thethiserrordependency to be added to yourCargo.toml(cargo add thiserror).
- NOTE:
- It also provides a
err!macro that is a shorthand forErr(anyhow::anyhow!(...))orErr(okerr::anyerr!(...)). - It also provides a
fail!macro that isanyhow::bail!. - It also provides a
anyerr!macro that isanyhow::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::Erroris an alias ofthiserror::Error.
Macros§
- anyerr
- Same as
anyhow!(and its alias:format_err!). - anyhow
- Construct an ad-hoc error from a string or existing non-
anyhowerror value. - bail
- Return early with an error.
- ensure
- Return early with an error if a condition is not satisfied.
- err
- Shorthand for
Err(anyerr!(...))orErr(anyhow!(...)). - fail
- Same as
anyhow::bail!. - format_
err - Construct an ad-hoc error from a string or existing non-
anyhowerror value.
Structs§
- Chain
- Iterator of a chain of source errors.
- Error
- The
Errortype, a wrapper around a dynamic error type.
Traits§
- Context
- Provides the
contextmethod forResult.
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>