1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use serde_json::Error as JsonError;
use std::error::Error as StdError;
use std::fmt::{Display, Error as FmtError, Formatter, Result as FmtResult};
use std::result::Result as StdResult;

#[cfg(feature = "http")]
use http::uri::InvalidUri;
#[cfg(feature = "http")]
use http::Error as HttpError;
#[cfg(feature = "hyper")]
use hyper::error::Error as HyperError;
#[cfg(feature = "reqwest")]
use reqwest::{
    Error as ReqwestError,
    Response as ReqwestResponse,
    UrlError as ReqwestUrlError,
};

/// Standard result type for asynchronous functions throughout the library.
pub type Result<T> = StdResult<T, Error>;

/// Enum encompassing the library's possible returned errors.
#[derive(Debug)]
pub enum Error {
    /// An error that occurred while formatting a string.
    Fmt(FmtError),
    /// An error from the `serde_json` crate while deserializing the body of an
    /// HTTP response.
    Json(JsonError),
    /// An error from the `http` crate.
    #[cfg(feature = "http")]
    Http(HttpError),
    /// An error from the `hyper` crate while performing an HTTP request.
    #[cfg(feature = "hyper")]
    Hyper(HyperError),
    /// An error from the `reqwest` crate while performing an HTTP request.
    #[cfg(feature = "reqwest")]
    Reqwest(ReqwestError),
    /// An error indicating a bad request when using `reqwest`.
    #[cfg(feature = "reqwest")]
    ReqwestBad(Box<ReqwestResponse>),
    /// An error indicating an invalid request when using `reqwest`.
    #[cfg(feature = "reqwest")]
    ReqwestInvalid(Box<ReqwestResponse>),
    /// An error indicating a parsing issue when using `reqwest`.
    #[cfg(feature = "reqwest")]
    ReqwestParse(ReqwestUrlError),
    /// An error when building a request's URI from the `http` crate.
    #[cfg(feature = "http")]
    Uri(InvalidUri),
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str(self.description())
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Fmt(ref inner) => inner.description(),
            #[cfg(feature = "http")]
            Error::Http(ref inner) => inner.description(),
            #[cfg(feature = "hyper")]
            Error::Hyper(ref inner) => inner.description(),
            Error::Json(ref inner) => inner.description(),
            #[cfg(feature = "reqwest")]
            Error::Reqwest(ref inner) => inner.description(),
            #[cfg(feature = "reqwest")]
            Error::ReqwestBad(_) => "Request bad",
            #[cfg(feature = "reqwest")]
            Error::ReqwestInvalid(_) => "Request invalid",
            #[cfg(feature = "reqwest")]
            Error::ReqwestParse(ref inner) => inner.description(),
            #[cfg(feature = "http")]
            Error::Uri(ref inner) => inner.description(),
        }
    }
}

impl From<FmtError> for Error {
    fn from(err: FmtError) -> Self {
        Error::Fmt(err)
    }
}

impl From<JsonError> for Error {
    fn from(err: JsonError) -> Self {
        Error::Json(err)
    }
}

#[cfg(feature = "http")]
impl From<HttpError> for Error {
    fn from(err: HttpError) -> Self {
        Error::Http(err)
    }
}

#[cfg(feature = "hyper")]
impl From<HyperError> for Error {
    fn from(err: HyperError) -> Self {
        Error::Hyper(err)
    }
}

#[cfg(feature = "http")]
impl From<InvalidUri> for Error {
    fn from(err: InvalidUri) -> Error {
        Error::Uri(err)
    }
}

#[cfg(feature = "reqwest")]
impl From<ReqwestError> for Error {
    fn from(err: ReqwestError) -> Self {
        Error::Reqwest(err)
    }
}

#[cfg(feature = "reqwest")]
impl From<ReqwestUrlError> for Error {
    fn from(err: ReqwestUrlError) -> Self {
        Error::ReqwestParse(err)
    }
}