pub use hyper::Error as HyperError;
#[cfg(feature = "tls")]
pub use hyper_tls::Error as TlsError;
use std::error::{self, Error as _Error};
use std::fmt::{self, Display, Formatter};
use std::io;
use std::str::Utf8Error;
use types::StatusCode;
#[derive(Debug)]
pub enum Error {
Gzip(io::Error),
Http(StatusCode),
Hyper(HyperError),
TimedOut,
Utf8(Utf8Error),
Custom(Box<dyn error::Error + Send + Sync>),
}
impl Error {
pub fn custom<E>(error: E) -> Self
where
E: Into<Box<dyn error::Error + Send + Sync>>,
{
Error::Custom(error.into())
}
}
impl error::Error for Error {
fn description(&self) -> &str {
use Error::*;
match *self {
Gzip(ref e) => e.description(),
Http(ref status) => status.canonical_reason().unwrap_or("<unknown status code>"),
Hyper(ref e) => e.description(),
TimedOut => "timed out",
Utf8(ref e) => e.description(),
Custom(ref e) => e.description(),
}
}
fn cause(&self) -> Option<&dyn error::Error> {
use Error::*;
match *self {
Gzip(ref e) => Some(e),
Http(_) | TimedOut => None,
Hyper(ref e) => Some(e),
Utf8(ref e) => Some(e),
Custom(ref e) => Some(&**e),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use Error::*;
match *self {
Gzip(ref e) => Display::fmt(e, f),
Http(ref code) => Display::fmt(code, f),
Hyper(ref e) => Display::fmt(e, f),
TimedOut => Display::fmt(self.description(), f),
Utf8(ref e) => Display::fmt(e, f),
Custom(ref e) => Display::fmt(e, f),
}
}
}