6 releases (2 stable)

1.1.0 Aug 18, 2023
1.0.0 Aug 18, 2023
0.1.3 Feb 15, 2023
0.1.1 Dec 3, 2022
0.1.0 Oct 25, 2022

#1263 in Rust patterns

Download history 63/week @ 2024-11-15 110/week @ 2024-11-22 316/week @ 2024-11-29 627/week @ 2024-12-06 115/week @ 2024-12-13 55/week @ 2024-12-20 29/week @ 2024-12-27 456/week @ 2025-01-03 423/week @ 2025-01-10 173/week @ 2025-01-17 52/week @ 2025-01-24 87/week @ 2025-01-31 103/week @ 2025-02-07 218/week @ 2025-02-14 274/week @ 2025-02-21 211/week @ 2025-02-28

843 downloads per month
Used in 27 crates (21 directly)

MIT/Apache

11KB
187 lines

orfail

orfail Documentation Actions Status License

An error handling library for portable unrecoverable errors.

This crate provides,

  • Failure struct that represents an unrecoverable error with an error message and user-level backtrace
    • Constituted with simple types (u32, String, and Vec of those)
      • Portable across process and language boundaries
      • Optional serde support ("serde" feature)
    • Doesn't implement std::error::Error trait
  • OrFail trait
    • Backtrace location is appended to Failure each time when calling OrFail::or_fail()
    • bool, Option<_> and Result<_, _> implement OrFail

Examples

use orfail::{OrFail, Result};

fn check_non_zero(n: isize) -> Result<()> {
    (n != 0).or_fail()?;
    Ok(())
}

fn safe_div(x: isize, y: isize) -> Result<isize> {
    check_non_zero(y).or_fail()?;
    Ok(x / y)
}

// OK
assert_eq!(safe_div(4, 2), Ok(2));

// NG
assert_eq!(safe_div(4, 0).err().map(|e| e.to_string()),
           Some(
r#"expected `true` but got `false`
  at src/lib.rs:8
  at src/lib.rs:13
"#.to_owned()));

Dependencies

~155KB