eyre-rs / color-eyre

Custom hooks for colorful human oriented error reports via panics and the eyre crate
Other
960 stars 56 forks source link

Add context_from traits from zebra-test to upstream color-eyre crate #107

Closed yaahc closed 2 years ago

yaahc commented 2 years ago

I originally wrote this trait as part of the zebra-test^1 crate to abstract over the act of adding context from an external command to an error report in our various helper methods. I particularly like this pattern am curious to see if it applies cleanly to many other types during error reporting and want to add it to color-eyre proper so users of this crate can potentially implement it on their types.

TODO

Output

The following example:

fn visit_the_shell() -> eyre::Result<()> {
    let mut cmd = std::process::Command::new("bash");
    cmd.arg("-c")
        // uh oh, there's an extra ' in that string and bash isn't gonna like it!
        .arg("echo 'Hello bash, I hope you're doing well!'");
    let output = cmd.output().context_from(&cmd)?;
    if !output.status.success() {
        Err(eyre::eyre!("invalid bash command"))
            .context_from(&cmd)
            .context_from(&output)
    } else {
        // I know the command is invalid ;)
        Ok(())
    }
}

Produces the following output:

Screenshot from 2022-04-08 22-41-43