Skip to content

Commit 7ff0e8a

Browse files
authored
Pass through ureq::Error wrapped as io::Error
1 parent 6871bae commit 7ff0e8a

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

3-
* send_json should set content-length header (#983)
3+
* ureq::Error wrapped as io::Error should pass through body chain (#984)
4+
* send_json should set content-length header (#983)
45

56
# 3.0.4
67

src/body/brotli.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::io;
22

33
use brotli_decompressor::Decompressor;
44

5+
use crate::error::is_wrapped_ureq_error;
56
use crate::Error;
67

78
pub(crate) struct BrotliDecoder<R: io::Read>(Decompressor<R>);
@@ -14,8 +15,13 @@ impl<R: io::Read> BrotliDecoder<R> {
1415

1516
impl<R: io::Read> io::Read for BrotliDecoder<R> {
1617
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
17-
self.0
18-
.read(buf)
19-
.map_err(|e| Error::Decompress("brotli", e).into_io())
18+
self.0.read(buf).map_err(|e| {
19+
if is_wrapped_ureq_error(&e) {
20+
// If this already is a ureq::Error, like Timeout, pass it along.
21+
e
22+
} else {
23+
Error::Decompress("brotli", e).into_io()
24+
}
25+
})
2026
}
2127
}

src/body/gzip.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::io;
22

33
use flate2::read::MultiGzDecoder;
44

5+
use crate::error::is_wrapped_ureq_error;
56
use crate::Error;
67

78
pub(crate) struct GzipDecoder<R>(MultiGzDecoder<R>);
@@ -14,9 +15,14 @@ impl<R: io::Read> GzipDecoder<R> {
1415

1516
impl<R: io::Read> io::Read for GzipDecoder<R> {
1617
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
17-
self.0
18-
.read(buf)
19-
.map_err(|e| Error::Decompress("gzip", e).into_io())
18+
self.0.read(buf).map_err(|e| {
19+
if is_wrapped_ureq_error(&e) {
20+
// If this already is a ureq::Error, like Timeout, pass it along.
21+
e
22+
} else {
23+
Error::Decompress("gzip", e).into_io()
24+
}
25+
})
2026
}
2127
}
2228

src/error.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,13 @@ impl Error {
169169
}
170170
}
171171

172+
pub(crate) fn is_wrapped_ureq_error(e: &io::Error) -> bool {
173+
e.get_ref().map(|x| x.is::<Error>()).unwrap_or(false)
174+
}
175+
172176
impl From<io::Error> for Error {
173177
fn from(e: io::Error) -> Self {
174-
let is_wrapped_ureq_error = e.get_ref().map(|x| x.is::<Error>()).unwrap_or(false);
175-
176-
if is_wrapped_ureq_error {
178+
if is_wrapped_ureq_error(&e) {
177179
// unwraps are ok, see above.
178180
let boxed = e.into_inner().unwrap();
179181
let ureq = boxed.downcast::<Error>().unwrap();

0 commit comments

Comments
 (0)