Replies: 2 comments
-
You need to recursively search the whole error chain. Not just the first one. Try using something like this // attempt to downcast `err` into a `T` and if that fails recursively try and
// downcast `err`'s source
fn find_error_source<'a, T>(err: &'a (dyn Error + 'static)) -> Option<&'a T>
where
T: Error + 'static,
{
if let Some(err) = err.downcast_ref::<T>() {
Some(err)
} else if let Some(source) = err.source() {
find_error_source(source)
} else {
None
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think I know why. It's wrapped in a 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
}
} Thanks for the help! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying bidirectional stream.
When a client crashes, by polling
stream.next().await
, I get an error:I want to have special treatment to the error kind, say, adding metrics to record the event.
How can I write code to match the error?
I try the code, but it does not work.
thx.
Beta Was this translation helpful? Give feedback.
All reactions