Crates.io | problem |
lib.rs | problem |
version | 5.3.0 |
source | src |
created_at | 2018-08-16 14:01:47.946061 |
updated_at | 2019-12-12 17:02:01.655223 |
description | Error handling for command line applications or prototypes |
homepage | |
repository | https://github.com/jpastuszek/problem |
max_upload_size | |
id | 79766 |
size | 48,520 |
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:
error!
macro),panic!
macro),?
),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.
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.