Redesign Error type

- The `Error`'s kind is a now a set of variants depending on the context
  of when an error could occur.
- If another error was the cause, it is now always the `source`.

Along with the `is_*` methods, this should help in understanding *when*
a certain error occurred. For example, an error setting the TLS
certificates will return a builder error, with the TLS error as the
source. This should help differentiate from a TLS error that happens
when connecting to a server.

It also makes the internal code less dependent on all the exact
dependencies that can be enabled or disabled.
This commit is contained in:
Sean McArthur
2019-09-17 12:55:20 -07:00
parent 6b5be07158
commit 53495e1526
20 changed files with 209 additions and 544 deletions

View File

@@ -154,11 +154,11 @@ impl Stream for ImplStream {
} => {
if let Some(ref mut timeout) = timeout {
if let Poll::Ready(()) = Pin::new(timeout).poll(cx) {
return Poll::Ready(Some(Err(crate::error::timedout(None))));
return Poll::Ready(Some(Err(crate::error::body(crate::error::TimedOut))));
}
}
futures_core::ready!(Pin::new(body).poll_data(cx))
.map(|opt_chunk| opt_chunk.map(Into::into).map_err(crate::error::from))
.map(|opt_chunk| opt_chunk.map(Into::into).map_err(crate::error::body))
}
Inner::Reusable(ref mut bytes) => {
if bytes.is_empty() {

View File

@@ -746,13 +746,17 @@ impl Future for PendingRequest {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(delay) = self.as_mut().timeout().as_mut().as_pin_mut() {
if let Poll::Ready(()) = delay.poll(cx) {
return Poll::Ready(Err(crate::error::timedout(Some(self.url.clone()))));
return Poll::Ready(Err(
crate::error::request(crate::error::TimedOut).with_url(self.url.clone())
));
}
}
loop {
let res = match self.as_mut().in_flight().as_mut().poll(cx) {
Poll::Ready(Err(e)) => return Poll::Ready(url_error!(e, &self.url)),
Poll::Ready(Err(e)) => {
return Poll::Ready(Err(crate::error::request(e).with_url(self.url.clone())));
}
Poll::Ready(Ok(res)) => res,
Poll::Pending => return Poll::Pending,
};

View File

@@ -137,14 +137,14 @@ impl Stream for Decoder {
let new_value = match self.inner {
Inner::Pending(ref mut future) => match Pin::new(future).poll(cx) {
Poll::Ready(Ok(inner)) => inner,
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(crate::error::from_io(e)))),
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(crate::error::decode_io(e)))),
Poll::Pending => return Poll::Pending,
},
Inner::PlainText(ref mut body) => return Pin::new(body).poll_next(cx),
Inner::Gzip(ref mut decoder) => {
return match futures_core::ready!(Pin::new(decoder).poll_next(cx)) {
Some(Ok(bytes)) => Poll::Ready(Some(Ok(bytes))),
Some(Err(err)) => Poll::Ready(Some(Err(crate::error::from_io(err)))),
Some(Err(err)) => Poll::Ready(Some(Err(crate::error::decode_io(err)))),
None => Poll::Ready(None),
}
}

View File

@@ -203,7 +203,7 @@ impl Part {
/// Tries to set the mime of this part.
pub fn mime_str(self, mime: &str) -> crate::Result<Part> {
Ok(self.mime(mime.parse().map_err(crate::error::from)?))
Ok(self.mime(mime.parse().map_err(crate::error::builder)?))
}
// Re-export when mime 0.4 is available, with split MediaType/MediaRange.

View File

@@ -111,9 +111,9 @@ impl RequestBuilder {
Ok(value) => {
req.headers_mut().append(key, value);
}
Err(e) => error = Some(crate::error::from(e.into())),
Err(e) => error = Some(crate::error::builder(e.into())),
},
Err(e) => error = Some(crate::error::from(e.into())),
Err(e) => error = Some(crate::error::builder(e.into())),
};
}
if let Some(err) = error {
@@ -250,7 +250,7 @@ impl RequestBuilder {
let serializer = serde_urlencoded::Serializer::new(&mut pairs);
if let Err(err) = query.serialize(serializer) {
error = Some(crate::error::from(err));
error = Some(crate::error::builder(err));
}
}
if let Ok(ref mut req) = self.request {
@@ -276,7 +276,7 @@ impl RequestBuilder {
);
*req.body_mut() = Some(body.into());
}
Err(err) => error = Some(crate::error::from(err)),
Err(err) => error = Some(crate::error::builder(err)),
}
}
if let Some(err) = error {
@@ -300,7 +300,7 @@ impl RequestBuilder {
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
*req.body_mut() = Some(body.into());
}
Err(err) => error = Some(crate::error::from(err)),
Err(err) => error = Some(crate::error::builder(err)),
}
}
if let Some(err) = error {

View File

@@ -227,7 +227,7 @@ impl Response {
pub async fn json<T: DeserializeOwned>(self) -> crate::Result<T> {
let full = self.bytes().await?;
serde_json::from_slice(&full).map_err(crate::error::from)
serde_json::from_slice(&full).map_err(crate::error::decode)
}
/// Get the full response body as `Bytes`.