anyhow_report/anyhow_report.rs
1//! If your application already uses anyhow, there are several ways to use this
2//! library.
3//!
4//! 1. Replace every instance of any `anyhow` import with `narrate` (This could
5//! be tedious if you have a lot of contexts).
6//!
7//! 2. Only use `narrate::Error` in your outermost functions to use the help
8//! message feature.
9//!
10//! 3. Just use the `report` module to "pretty print" the anyhow errors.
11//!
12//! This is an example of the third method. It prints:
13//!
14//! ```console
15//! error: operating system error
16//! cause: inner error
17//! ```
18
19use anyhow::Context;
20use narrate::{report, CliError, ExitCode};
21
22fn main() {
23 let res = inner_fn().context(CliError::OsErr);
24
25 if let Err(ref err) = res {
26 report::anyhow_err_full(err);
27 // As the error contains a `CliError`, this code will match its
28 // error_code. In this case `71`. If there was no underlying `CliError`,
29 // the code will default to `70`.
30 std::process::exit(err.exit_code());
31 }
32}
33
34fn inner_fn() -> anyhow::Result<()> {
35 anyhow::bail!("inner error")
36}