12 stable releases (5 major)

Uses old Rust 2015

5.3.0 Dec 12, 2019
5.2.1 Nov 22, 2019
5.2.0 Oct 18, 2019
5.1.0 Apr 30, 2019
0.1.0 Aug 16, 2018

#462 in Rust patterns

Download history 44/week @ 2023-12-22 30/week @ 2023-12-29 65/week @ 2024-01-05 66/week @ 2024-01-12 40/week @ 2024-01-19 51/week @ 2024-01-26 35/week @ 2024-02-02 51/week @ 2024-02-09 61/week @ 2024-02-16 44/week @ 2024-02-23 60/week @ 2024-03-01 75/week @ 2024-03-08 62/week @ 2024-03-15 74/week @ 2024-03-22 116/week @ 2024-03-29 45/week @ 2024-04-05

308 downloads per month
Used in 14 crates (7 directly)

MIT license

48KB
662 lines

Latest Version Documentation License

The aim of this library is to help writing command line programs or prototypes more efficiently in Rust by simplifying error handling in program code.

This crate introduces Problem type which can be used on high level APIs for which error handling boils down to:

  • reporting error message (e.g. log with error! macro),
  • aborting program on error other than a bug (e.g. using panic! macro),
  • bubbling up errors (e.g. with ?),
  • ignoring errors (e.g. using Result::ok).

Goals

  • Simplify signatures of functions that can fail to one error type so they compose easier.
  • Produce detailed user friendly error messages with Display formatting including error cause chain and backtrace (when requested).
  • Make it convenient to add context to error message in different situations.
  • Make it convenient to report (print or log) or abort program on error in different situations.
  • Make it easy to construct Problem ad-hock (e.g. from string literal).
  • Low effort integration with existing error types and program flows.
  • Minimize performance impact on good path.
  • Allow errors to bubble up by elimination of lifetimes in external error types.

Non-goals

  • Providing ability to match on particular error variant to facilitate recovering from error condition.
  • Zero cost error path - e.g. no allocation.
  • Sync or Send compatibility.

Example

Implicit conversion to Problem type and context message.

use problem::prelude::*;

fn foo() -> Result<String, Problem> {
    let str = String::from_utf8(vec![0, 123, 255])?;
    Ok(str)
}

let result = foo().problem_while("creating string");

assert_eq!(result.unwrap_err().to_string(), "while creating string got error caused by: invalid utf-8 sequence of 1 bytes from index 2");

Handling fatal errors with panic using .or_failed_to().

use problem::prelude::*;
use problem::format_panic_to_stderr;

// Replace Rust default panic handler
format_panic_to_stderr();

fn foo() -> Result<String, Problem> {
    let str = String::from_utf8(vec![0, 123, 255])?;
    Ok(str)
}

let _s = foo().or_failed_to("create a string"); // Fatal error: Panicked in src/lib.rs:464:25: Failed to create a string due to: invalid utf-8 sequence of 1 bytes from index 2

For more examples see crate documentation at docs.rs.

If you are looking for fully typed, zero-cost way of adding context to error values see error-context crate.

Dependencies

~2.5–3.5MB
~73K SLoC