Skip to content

Commit

Permalink
Error type updates (#258)
Browse files Browse the repository at this point in the history
Update `isahc::Error` to support taking new variants in a backwards-compatible way, as well as hold more error sources when an error is caused by another.

Note that this is the first 1.0 breaking change to land on `master`. If 0.9 needs a patch fix in the future, we'll have to make a 0.9.x branch for it.

Fixes #182.
  • Loading branch information
sagebind authored Nov 24, 2020
1 parent e7284e3 commit 5212209
Show file tree
Hide file tree
Showing 17 changed files with 435 additions and 218 deletions.
11 changes: 7 additions & 4 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
//! Since request executions are driven through futures, the agent also acts as
//! a specialized task executor for tasks related to requests.
use crate::handler::RequestHandler;
use crate::task::{UdpWaker, WakerExt};
use crate::Error;
use crate::{
error::Error,
handler::RequestHandler,
task::{UdpWaker, WakerExt},
};
use crossbeam_utils::sync::WaitGroup;
use curl::multi::WaitFd;
use flume::{Receiver, Sender};
use slab::Slab;
use std::{
io,
net::UdpSocket,
sync::Mutex,
task::Waker,
Expand Down Expand Up @@ -54,7 +57,7 @@ impl AgentBuilder {

/// Spawn a new agent using the configuration in this builder and return a
/// handle for communicating with the agent.
pub(crate) fn spawn(&self) -> Result<Handle, Error> {
pub(crate) fn spawn(&self) -> io::Result<Handle> {
let create_start = Instant::now();

// Initialize libcurl, if necessary, on the current thread.
Expand Down
22 changes: 0 additions & 22 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,6 @@ use std::{
task::{Context, Poll},
};

macro_rules! match_type {
{
$(
<$name:ident as $T:ty> => $branch:expr,
)*
$defaultName:ident => $defaultBranch:expr,
} => {{
match () {
$(
_ if ::std::any::Any::type_id(&$name) == ::std::any::TypeId::of::<$T>() => {
#[allow(unsafe_code)]
let $name: $T = unsafe {
::std::mem::transmute_copy::<_, $T>(&::std::mem::ManuallyDrop::new($name))
};
$branch
}
)*
_ => $defaultBranch,
}
}};
}

/// Contains the body of an HTTP request or response.
///
/// This type is used to encapsulate the underlying stream or region of memory
Expand Down
20 changes: 13 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
use crate::{
agent::{self, AgentBuilder},
auth::{Authentication, Credentials},
body::Body,
config::internal::{ConfigurableBase, SetOpt},
config::*,
default_headers::DefaultHeadersInterceptor,
error::{Error, ErrorKind},
handler::{RequestHandler, ResponseBodyReader},
headers,
interceptor::{self, Interceptor, InterceptorObj},
Body, Error,
};
use futures_lite::{future::block_on, io::AsyncRead, pin};
use http::{
Expand Down Expand Up @@ -343,11 +344,11 @@ impl HttpClientBuilder {
self.default_headers.append(key, value);
}
Err(e) => {
self.error = Some(e.into().into());
self.error = Some(Error::new(ErrorKind::ClientInitialization, e.into()));
}
},
Err(e) => {
self.error = Some(e.into().into());
self.error = Some(Error::new(ErrorKind::ClientInitialization, e.into()));
}
}
self
Expand Down Expand Up @@ -450,20 +451,22 @@ impl HttpClientBuilder {

#[cfg(not(feature = "cookies"))]
let inner = Inner {
agent: self.agent_builder.spawn()?,
agent: self.agent_builder.spawn().map_err(|e| Error::new(ErrorKind::ClientInitialization, e))?,
defaults: self.defaults,
interceptors: self.interceptors,
};

#[cfg(feature = "cookies")]
let inner = Inner {
agent: self.agent_builder.spawn()?,
agent: self.agent_builder.spawn().map_err(|e| Error::new(ErrorKind::ClientInitialization, e))?,
defaults: self.defaults,
interceptors: self.interceptors,
cookie_jar: self.cookie_jar,
};

Ok(HttpClient { inner: Arc::new(inner) })
Ok(HttpClient {
inner: Arc::new(inner),
})
}
}

Expand Down Expand Up @@ -896,7 +899,10 @@ impl HttpClient {
builder: http::request::Builder,
body: Body,
) -> ResponseFuture<'_> {
ResponseFuture::new(async move { self.send_async_inner(builder.body(body)?).await })
ResponseFuture::new(async move {
self.send_async_inner(builder.body(body).map_err(Error::from_any)?)
.await
})
}

/// Actually send the request. All the public methods go through here.
Expand Down
3 changes: 2 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ pub trait Configurable: internal::ConfigurableBase {
/// decode the HTTP response body for known and available compression
/// algorithms. If the server returns a response with an unknown or
/// unavailable encoding, Isahc will return an
/// [`InvalidContentEncoding`](crate::Error::InvalidContentEncoding) error.
/// [`InvalidContentEncoding`](crate::error::ErrorKind::InvalidContentEncoding)
/// error.
///
/// If you do not specify a specific value for the
/// [`Accept-Encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding)
Expand Down
Loading

0 comments on commit 5212209

Please sign in to comment.