improve error messages from gzip decoder

This commit is contained in:
Sean McArthur
2019-04-25 10:28:13 -07:00
parent 6fe3d6a8a0
commit e4b91ad201
2 changed files with 62 additions and 20 deletions

View File

@@ -484,6 +484,18 @@ pub(crate) fn into_io(e: Error) -> io::Error {
}
}
pub(crate) fn from_io(e: io::Error) -> Error {
if e.get_ref().map(|r| r.is::<Error>()).unwrap_or(false) {
*e
.into_inner()
.expect("io::Error::get_ref was Some(_)")
.downcast::<Error>()
.expect("StdError::is() was true")
} else {
from(e)
}
}
macro_rules! try_ {
($e:expr) => (
@@ -504,6 +516,20 @@ macro_rules! try_ {
)
}
macro_rules! try_io {
($e:expr) => (
match $e {
Ok(v) => v,
Err(ref err) if err.kind() == ::std::io::ErrorKind::WouldBlock => {
return Ok(::futures::Async::NotReady);
}
Err(err) => {
return Err(::error::from_io(err));
}
}
)
}
pub(crate) fn loop_detected(url: Url) -> Error {
Error::new(Kind::RedirectLoop, Some(url))
}
@@ -594,4 +620,18 @@ mod tests {
use std::mem::size_of;
assert_eq!(size_of::<Error>(), size_of::<usize>());
}
#[test]
fn roundtrip_io_error() {
let orig = unknown_proxy_scheme();
// Convert reqwest::Error into an io::Error...
let io = into_io(orig);
// Convert that io::Error back into a reqwest::Error...
let err = from_io(io);
// It should have pulled out the original, not nested it...
match err.inner.kind {
Kind::UnknownProxyScheme => (),
_ => panic!("{:?}", err),
}
}
}