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

Reporting multiple errors #108

Closed Kyuuhachi closed 2 years ago

Kyuuhachi commented 2 years ago

In some cases it might be useful to bundle several errors into one, displaying them all together. A few prior example are Python's PEP 654, Trio's MultiError (which inspired the former), and Haskell's Validate monad. Would something like that be possible and/or reasonable to add? Asking here rather than eyre because section is here, so it feels appropriate.

I can get a mediocre implementation by doing

    use std::fmt::Write;
    use color_eyre::{Section, SectionExt};
    match errors.len() {
        0 => Ok(result),
        1 => Err(errors.pop().unwrap()),
        _ => Err(eyre::eyre!("Multiple errors").section({
            let mut s = String::new();
            for e in errors {
                write!(s, "{:?}", e).unwrap();
            }
            s.header("Errors:")
        })),
    }

, but the formatting is pretty bad and contains much duplication; in particular, trimming off the shared tail of the backtraces would make it much better.

yaahc commented 2 years ago

This is already supported: https://github.com/yaahc/color-eyre/blob/master/examples/multiple_errors.rs

https://docs.rs/color-eyre/latest/color_eyre/section/trait.Section.html#tymethod.error