Skip to content

Commit

Permalink
Pass through ureq::Error wrapped as io::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Feb 3, 2025
1 parent c7a9c9e commit efd0f44
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/body/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;

use brotli_decompressor::Decompressor;

use crate::error::is_wrapped_ureq_error;
use crate::Error;

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

impl<R: io::Read> io::Read for BrotliDecoder<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0
.read(buf)
.map_err(|e| Error::Decompress("brotli", e).into_io())
self.0.read(buf).map_err(|e| {
if is_wrapped_ureq_error(&e) {
// If this already is a ureq::Error, like Timeout, pass it along.
e
} else {
Error::Decompress("brotli", e).into_io()
}
})
}
}
12 changes: 9 additions & 3 deletions src/body/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;

use flate2::read::MultiGzDecoder;

use crate::error::is_wrapped_ureq_error;
use crate::Error;

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

impl<R: io::Read> io::Read for GzipDecoder<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0
.read(buf)
.map_err(|e| Error::Decompress("gzip", e).into_io())
self.0.read(buf).map_err(|e| {
if is_wrapped_ureq_error(&e) {
// If this already is a ureq::Error, like Timeout, pass it along.
e
} else {
Error::Decompress("gzip", e).into_io()
}
})
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ impl Error {
}
}

pub(crate) fn is_wrapped_ureq_error(e: &io::Error) -> bool {
e.get_ref().map(|x| x.is::<Error>()).unwrap_or(false)
}

impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
let is_wrapped_ureq_error = e.get_ref().map(|x| x.is::<Error>()).unwrap_or(false);

if is_wrapped_ureq_error {
if is_wrapped_ureq_error(&e) {
// unwraps are ok, see above.
let boxed = e.into_inner().unwrap();
let ureq = boxed.downcast::<Error>().unwrap();
Expand Down

0 comments on commit efd0f44

Please sign in to comment.