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

Pass through ureq::Error wrapped as io::Error #984

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

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

# 3.0.4

Expand Down
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
Loading