Function narrate::report::err_full

source ·
pub fn err_full(err: &Error)
Expand description

Report an Error to stderr, printing a list of causes

The message will consist of a red error: title, followed by the Display impl for the underlying error. Each subsequent wrapped error will have a plain cause: title.

Examples

Wrapped error.

use narrate::{report, ErrorWrap, Result};

fn setup_config() -> Result<()> {
    ...
    let user_config = parse_config_file(&path)
        .wrap_with(|| format!("invalid config file: `{}`", &path))?;
    ...
}

fn main() {
    ...
    let res = setup_config().wrap("invalid configuration");
    if let Err(ref err) = res {
        report::err_full(err);
        // error: invalid configuration
        // cause: invalid config file: `config.toml`
        // cause: missing key: `author`
    }
    ...
}

Wrapped error with a help message.

use std::{fs::File, path::PathBuf};

use narrate::{report, CliError, ErrorWrap, Result};

fn run() -> Result<()> {
    let path = PathBuf::from("/nopermission/file.txt");
    File::create(&path)
        .wrap_with(|| CliError::CreateFile(path))
        .add_help("try using a valid file name")?;
    Ok(())
}


fn main() {
    if let Err(err) = run() {
        report::err_full(&err);
        // error: cannot create file: "/nopermission.txt"
        // cause: permission denied
        //
        // try using a valid file name
    }
}
Examples found in repository?
examples/lazy_wrap.rs (line 23)
18
19
20
21
22
23
24
25
26
fn main() {
    let path = PathBuf::from("/not/an/exsisting/file");
    let res = run(path);

    if let Err(ref err) = res {
        report::err_full(err);
        std::process::exit(err.exit_code());
    }
}
More examples
Hide additional examples
examples/wrap_report_exit.rs (line 14)
8
9
10
11
12
13
14
15
16
17
fn main() {
    let res = config_error()
        .wrap(CliError::Config)
        .add_help("See https://docs.example.rs/config for more info");

    if let Err(ref err) = res {
        report::err_full(err);
        std::process::exit(err.exit_code());
    }
}