wrap_report_exit/
wrap_report_exit.rs

1//! - Wraps errors in string and error contexts.
2//! - Adds help text
3//! - Reports to stderr
4//! - Exits with error code
5
6use narrate::{report, CliError, Error, ErrorWrap, ExitCode, Result};
7
8fn main() {
9    let res = config_error()
10        .wrap(CliError::Config)
11        .add_help("See https://docs.example.rs/config for more info");
12
13    if let Err(ref err) = res {
14        report::err_full(err);
15        std::process::exit(err.exit_code());
16    }
17}
18
19/// Equivalent to:
20/// ```no_run
21/// serde_json::from_str(&json)
22///     .wrap(|| format!("bad config file `{}`", path))
23/// ```
24fn config_error() -> Result<(), Error> {
25    // simulate deserialization error
26    let error = narrate::error_from!("missing key: 'port'");
27    // wrap with config error
28    let error = error.wrap("bad config file `/app/config.toml`");
29
30    Err(error)
31}