#![cfg_attr(target_arch = "wasm32", allow(unused))]
use std::error::Error as StdError;
use std::fmt;
use std::io;
use crate::{StatusCode, Url};
pub type Result<T> = std::result::Result<T, Error>;
pub struct Error {
inner: Box<Inner>,
}
pub(crate) type BoxError = Box<dyn StdError + Send + Sync>;
struct Inner {
kind: Kind,
source: Option<BoxError>,
url: Option<Url>,
}
impl Error {
pub(crate) fn new<E>(kind: Kind, source: Option<E>) -> Error
where
E: Into<BoxError>,
{
Error {
inner: Box::new(Inner {
kind,
source: source.map(Into::into),
url: None,
}),
}
}
pub fn url(&self) -> Option<&Url> {
self.inner.url.as_ref()
}
pub fn url_mut(&mut self) -> Option<&mut Url> {
self.inner.url.as_mut()
}
pub fn with_url(mut self, url: Url) -> Self {
self.inner.url = Some(url);
self
}
pub fn without_url(mut self) -> Self {
self.inner.url = None;
self
}
pub fn is_builder(&self) -> bool {
matches!(self.inner.kind, Kind::Builder)
}
pub fn is_redirect(&self) -> bool {
matches!(self.inner.kind, Kind::Redirect)
}
pub fn is_status(&self) -> bool {
matches!(self.inner.kind, Kind::Status(_))
}
pub fn is_timeout(&self) -> bool {
let mut source = self.source();
while let Some(err) = source {
if err.is::<TimedOut>() {
return true;
}
source = err.source();
}
false
}
pub fn is_request(&self) -> bool {
matches!(self.inner.kind, Kind::Request)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn is_connect(&self) -> bool {
let mut source = self.source();
while let Some(err) = source {
if let Some(hyper_err) = err.downcast_ref::<hyper::Error>() {
if hyper_err.is_connect() {
return true;
}
}
source = err.source();
}
false
}
pub fn is_body(&self) -> bool {
matches!(self.inner.kind, Kind::Body)
}
pub fn is_decode(&self) -> bool {
matches!(self.inner.kind, Kind::Decode)
}
pub fn status(&self) -> Option<StatusCode> {
match self.inner.kind {
Kind::Status(code) => Some(code),
_ => None,
}
}
#[allow(unused)]
pub(crate) fn into_io(self) -> io::Error {
io::Error::new(io::ErrorKind::Other, self)
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("reqwest::Error");
builder.field("kind", &self.inner.kind);
if let Some(ref url) = self.inner.url {
builder.field("url", url);
}
if let Some(ref source) = self.inner.source {
builder.field("source", source);
}
builder.finish()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.inner.kind {
Kind::Builder => f.write_str("builder error")?,
Kind::Request => f.write_str("error sending request")?,
Kind::Body => f.write_str("request or response body error")?,
Kind::Decode => f.write_str("error decoding response body")?,
Kind::Redirect => f.write_str("error following redirect")?,
Kind::Upgrade => f.write_str("error upgrading connection")?,
Kind::Status(ref code) => {
let prefix = if code.is_client_error() {
"HTTP status client error"
} else {
debug_assert!(code.is_server_error());
"HTTP status server error"
};
write!(f, "{} ({})", prefix, code)?;
}
};
if let Some(url) = &self.inner.url {
write!(f, " for url ({})", url.as_str())?;
}
if let Some(e) = &self.inner.source {
write!(f, ": {}", e)?;
}
Ok(())
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.inner.source.as_ref().map(|e| &**e as _)
}
}
#[cfg(target_arch = "wasm32")]
impl From<crate::error::Error> for wasm_bindgen::JsValue {
fn from(err: Error) -> wasm_bindgen::JsValue {
js_sys::Error::from(err).into()
}
}
#[cfg(target_arch = "wasm32")]
impl From<crate::error::Error> for js_sys::Error {
fn from(err: Error) -> js_sys::Error {
js_sys::Error::new(&format!("{}", err))
}
}
#[derive(Debug)]
pub(crate) enum Kind {
Builder,
Request,
Redirect,
Status(StatusCode),
Body,
Decode,
Upgrade,
}
pub(crate) fn builder<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Builder, Some(e))
}
pub(crate) fn body<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Body, Some(e))
}
pub(crate) fn decode<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Decode, Some(e))
}
pub(crate) fn request<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Request, Some(e))
}
pub(crate) fn redirect<E: Into<BoxError>>(e: E, url: Url) -> Error {
Error::new(Kind::Redirect, Some(e)).with_url(url)
}
pub(crate) fn status_code(url: Url, status: StatusCode) -> Error {
Error::new(Kind::Status(status), None::<Error>).with_url(url)
}
pub(crate) fn url_bad_scheme(url: Url) -> Error {
Error::new(Kind::Builder, Some(BadScheme)).with_url(url)
}
if_wasm! {
pub(crate) fn wasm(js_val: wasm_bindgen::JsValue) -> BoxError {
format!("{:?}", js_val).into()
}
}
pub(crate) fn upgrade<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Upgrade, Some(e))
}
#[allow(unused)]
pub(crate) fn into_io(e: Error) -> io::Error {
e.into_io()
}
#[allow(unused)]
pub(crate) fn decode_io(e: io::Error) -> Error {
if e.get_ref().map(|r| r.is::<Error>()).unwrap_or(false) {
*e.into_inner()
.expect("io::Error::get_ref was Some(_)")
.downcast::<Error>()
.expect("StdError::is() was true")
} else {
decode(e)
}
}
#[derive(Debug)]
pub(crate) struct TimedOut;
impl fmt::Display for TimedOut {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("operation timed out")
}
}
impl StdError for TimedOut {}
#[derive(Debug)]
pub(crate) struct BadScheme;
impl fmt::Display for BadScheme {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("URL scheme is not allowed")
}
}
impl StdError for BadScheme {}
#[cfg(test)]
mod tests {
use super::*;
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
#[test]
fn test_source_chain() {
let root = Error::new(Kind::Request, None::<Error>);
assert!(root.source().is_none());
let link = super::body(root);
assert!(link.source().is_some());
assert_send::<Error>();
assert_sync::<Error>();
}
#[test]
fn mem_size_of() {
use std::mem::size_of;
assert_eq!(size_of::<Error>(), size_of::<usize>());
}
#[test]
fn roundtrip_io_error() {
let orig = super::request("orig");
let io = orig.into_io();
let err = super::decode_io(io);
match err.inner.kind {
Kind::Request => (),
_ => panic!("{:?}", err),
}
}
#[test]
fn from_unknown_io_error() {
let orig = io::Error::new(io::ErrorKind::Other, "orly");
let err = super::decode_io(orig);
match err.inner.kind {
Kind::Decode => (),
_ => panic!("{:?}", err),
}
}
#[test]
fn is_timeout() {
let err = super::request(super::TimedOut);
assert!(err.is_timeout());
let io = io::Error::new(io::ErrorKind::Other, err);
let nested = super::request(io);
assert!(nested.is_timeout());
}
}