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

#481 in Rust patterns

Download history 44/week @ 2024-06-10 50/week @ 2024-06-17 54/week @ 2024-06-24 2/week @ 2024-07-01 25/week @ 2024-07-08 59/week @ 2024-07-15 35/week @ 2024-07-22 70/week @ 2024-07-29 59/week @ 2024-08-05 42/week @ 2024-08-12 33/week @ 2024-08-19 80/week @ 2024-08-26 17/week @ 2024-09-02 15/week @ 2024-09-09 25/week @ 2024-09-16 74/week @ 2024-09-23

156 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.4–8MB
~69K SLoC