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

Add Error::is_connection_reset #2029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<io::Error>() {
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)
Expand Down Expand Up @@ -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());
}
}