File: multiple_errors.rs

package info (click to toggle)
rust-color-eyre 0.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,060 kB
  • sloc: python: 92; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,527 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use color_eyre::{eyre::eyre, eyre::Report, Section};
use thiserror::Error;

fn main() -> Result<(), Report> {
    color_eyre::install()?;
    let errors = get_errors();
    join_errors(errors)
}

fn join_errors(results: Vec<Result<(), SourceError>>) -> Result<(), Report> {
    if results.iter().all(|r| r.is_ok()) {
        return Ok(());
    }

    let err = results
        .into_iter()
        .filter(Result::is_err)
        .map(Result::unwrap_err)
        .fold(eyre!("encountered multiple errors"), |report, e| {
            report.error(e)
        });

    Err(err)
}

/// Helper function to generate errors
fn get_errors() -> Vec<Result<(), SourceError>> {
    vec![
        Err(SourceError {
            source: StrError("The task you ran encountered an error"),
            msg: "The task could not be completed",
        }),
        Err(SourceError {
            source: StrError("The machine you're connecting to is actively on fire"),
            msg: "The machine is unreachable",
        }),
        Err(SourceError {
            source: StrError("The file you're parsing is literally written in c++ instead of rust, what the hell"),
            msg: "The file could not be parsed",
        }),
    ]
}

/// Arbitrary error type for demonstration purposes
#[derive(Debug, Error)]
#[error("{0}")]
struct StrError(&'static str);

/// Arbitrary error type for demonstration purposes with a source error
#[derive(Debug, Error)]
#[error("{msg}")]
struct SourceError {
    msg: &'static str,
    source: StrError,
}