h2 icon indicating copy to clipboard operation
h2 copied to clipboard

Implement `Error::source` for `h2::Error`.

Open goffrie opened this issue 4 years ago • 4 comments

This makes it easier to detect an IO error at the bottom of an error cause chain (e.g. behind a hyper::Error).

Also, don't implement std::error::Error for the private error types, to avoid confusion.

goffrie avatar Apr 30 '20 06:04 goffrie

Can we push this forward?

In my case, when a client crashes, the server gets an error similar to this.

// dbg!(&source);

 &source = hyper::Error(
    Body,
    Error {
        kind: Io(
            Kind(
                BrokenPipe,
            ),
        ),
    },
)

I want to match the error kind to record the event, but recursively downcasting the error to std::io::Error couldn't work because h2::Error does not implement std::error::Error#source().

This is what I end up with:

fn match_broken_pipe_error(err: &(dyn std::error::Error + 'static)) -> bool {
    if let Some(e) = err.downcast_ref::<h2::Error>() {
        if let Some(e) = e.get_io() {
            if e.kind() == std::io::ErrorKind::BrokenPipe {
                return true;
            }
        }
    }

    if let Some(source) = err.source() {
        match_broken_pipe_error(source)
    } else {
        false
    }
}

The code is much more awkward.

alanhe avatar Jan 27 '22 01:01 alanhe

oof...any update on this? Ran into this myself

kaifastromai avatar Aug 16 '23 13:08 kaifastromai

This is still an issue esp while matching on a stream disconnect in tonic: https://github.com/hyperium/tonic/issues/1579 @seanmonstar what is needed to move this PR forward, happy to start a separate feature branch? This will still affect grpc disonnects since tonic uses h2 0.3 (so it would have to be backported as well).

mkatychev avatar Mar 18 '24 19:03 mkatychev

I've updated the PR for recent master if anyone would like to review this again :)

goffrie avatar Apr 29 '24 18:04 goffrie