Add Error::is_timeout() accessor

This commit is contained in:
Sean McArthur
2019-02-20 12:54:46 -08:00
parent 9e2b56ba56
commit cd0e4b3c2e

View File

@@ -163,6 +163,23 @@ impl Error {
} }
} }
/// Returns true if the error is related to a timeout.
pub fn is_timeout(&self) -> bool {
match self.inner.kind {
Kind::Io(ref io) => io.kind() == io::ErrorKind::TimedOut,
Kind::Hyper(ref error) => {
error
.cause2()
.and_then(|cause| {
cause.downcast_ref::<io::Error>()
})
.map(|io| io.kind() == io::ErrorKind::TimedOut)
.unwrap_or(false)
},
_ => false,
}
}
/// Returns true if the error is serialization related. /// Returns true if the error is serialization related.
#[inline] #[inline]
pub fn is_serialization(&self) -> bool { pub fn is_serialization(&self) -> bool {
@@ -411,12 +428,6 @@ where T: Into<Kind> {
} }
} }
#[cfg(unix)]
fn io_timeout() -> io::Error {
io::Error::new(io::ErrorKind::WouldBlock, "timed out")
}
#[cfg(windows)]
fn io_timeout() -> io::Error { fn io_timeout() -> io::Error {
io::Error::new(io::ErrorKind::TimedOut, "timed out") io::Error::new(io::ErrorKind::TimedOut, "timed out")
} }