diff --git a/src/error.rs b/src/error.rs index 9453fcd92..efc3f7a72 100644 --- a/src/error.rs +++ b/src/error.rs @@ -116,6 +116,22 @@ impl Error { false } + /// Returns true if the error is related to a connection reset. + pub fn is_connection_reset(&self) -> bool { + let mut source = self.source(); + + while let Some(err) = source { + if let Some(io) = err.downcast_ref::() { + if io.kind() == io::ErrorKind::ConnectionReset { + return true; + } + } + source = err.source(); + } + + false + } + /// Returns true if the error is related to the request pub fn is_request(&self) -> bool { matches!(self.inner.kind, Kind::Request) @@ -385,4 +401,17 @@ mod tests { let nested = super::request(io); assert!(nested.is_timeout()); } + + #[test] + fn is_connection_reset() { + let err = super::request(io::Error::new( + io::ErrorKind::ConnectionReset, + "connection reset", + )); + assert!(err.is_connection_reset()); + + let io = io::Error::new(io::ErrorKind::Other, err); + let nested = super::request(io); + assert!(nested.is_connection_reset()); + } }