miette icon indicating copy to clipboard operation
miette copied to clipboard

Wrapping errors looses diagnostic information

Open LMH01 opened this issue 1 year ago • 0 comments

I observed that diagnostic information is not rendered when an error is wrapped using wrap_err() even though it should be (if I understand #170 and #172 correctly).

Example:

fn main() -> Result<()> {
    let source_error = SourceError {};

    let caused_error: CausedErrorA = CausedErrorA {
        reason: source_error
    };

    let main_error = MainError {
        src: NamedSource::new(
            "test.txt",
            "This is a test\nMore Test Code\nThis is the third line",
        ),
        bad_bit: (0, 4).into(),
        reason: caused_error,
    };
    //Err(main_error).wrap_err("Outer a").wrap_err("Outer b").wrap_err("Outer c")?;
    Err(main_error)?;
    Ok(())
}

#[derive(Debug, Diagnostic, Error)]
#[error("Main error")]
#[diagnostic(code("1"))]
struct MainError {
    #[source_code]
    src: NamedSource,
    #[label("here")]
    bad_bit: SourceSpan,
    #[source]
    #[diagnostic_source]
    reason: CausedErrorA,
}

#[derive(Debug, Diagnostic, Error)]
#[error("Caused error a")]
#[diagnostic(code("2"), help("This should not happen"))]
struct CausedErrorA {
    #[source]
    #[diagnostic_source]
    reason: SourceError
}

#[derive(Debug, Diagnostic, Error)]
#[error("source error")]
#[diagnostic(code("3"), help("Hello, World!"))]
struct SourceError {

}

Running this yields the following output: image

If we instead wrap the resulting error in some error messages we receive the following output: image

From my understanding this is wrong because the diagnostics of SourceError and CausedErrorA are not displayed like in the first picture.

I'm using version v5.10.0, downgrading to v5.9.0 did not fix the issue. It seems like this issue should have been fixed/implemented in #170.

LMH01 avatar Aug 21 '23 15:08 LMH01