![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
).
Display
formatting including error cause chain and backtrace (when requested).Problem
ad-hock (e.g. from string literal).Sync
or Send
compatibility.Implicit conversion to Problem
type and context message.
```rust,skt-problem use problem::prelude::*;
fn foo() -> Result
let result = foo().problem_while("creating string");
asserteq!(result.unwraperr().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()
.
```rust,shouldpanic,skt-problem use problem::prelude::*; use problem::formatpanictostderr;
// Replace Rust default panic handler formatpanicto_stderr();
fn foo() -> Result
let s = foo().orfailed_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.