Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on sections and accumulating errors #193

Open
stackmystack opened this issue Aug 15, 2024 · 1 comment
Open

Question on sections and accumulating errors #193

stackmystack opened this issue Aug 15, 2024 · 1 comment

Comments

@stackmystack
Copy link

Hello,

I have somewhere in my program:

    let mut errs = Vec::new();
    while let Some(msg) = rx.recv().await {
        if let Err(err) = msg {
            errs.push(err);
        }
    }
    if errs.is_empty() {
        Ok(())
    } else {
        Err(errs
            .into_iter()
            .fold(eyre!("Build Failed"), |acc, err| acc.error(err)))
    }

Where the definition of the error I'm accumulating is:

#[derive(Debug, Error)]
#[error("{name}: {kind}")]
pub struct ParserError {
    name: String,
    kind: ParserOp,
    #[source]
    source: color_eyre::eyre::Error,
}

The field source will have an errors generated with sections like this (basically taken from your example):

            Err(msg
                .with_section(move || stdout.trim().to_string().header("Stdout:"))
                .with_section(move || stderr.trim().to_string().header("Stderr:")))

But when the error is printed, all sections are lost.
I'm trying to look at the docs and the code, but I'm either missing something, or it's expected and I'm not understanding how the lib works.

Is there a way to achieve what I'm expecting, maybe differently?

@yaahc
Copy link
Collaborator

yaahc commented Aug 15, 2024

I believe the problem is that when you treat an eyre::Report as a source error it uses the Display impl instead of the Debug impl to get the error message. The debug impl is where the color_eyre::EyreHandler prints the full error report including sources and additional context, the Display impl will only print the error message from that specific error with the expectation that some other higher level reporter will then iterate over its sources and format a full and cohesive error report.

Depending on exactly you'd like the report to look there are probably a few ways you can achieve this, though nothing built into the library as is. Off of the top of my head you can either manually iterate over those sources and print them via debug instead of folding them all into another eyre::Report, or you could integrate such logic into your own custom https://docs.rs/eyre/latest/eyre/trait.EyreHandler.html, though this would unfortunately require duplicating a lot of code from color-eyre to preserve the rest of the functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants