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
use std::error::Error as StdError;
use std::fmt;

/// The Errors that may occur when processing a `Request`.
#[derive(Debug)]
pub enum Error {
    /// An HTTP error from the `hyper` crate.
    Http(::hyper::Error),
    /// An error trying to serialize a value.
    ///
    /// This may be serializing a value that is illegal in JSON or
    /// form-url-encoded bodies.
    Serialize(Box<StdError + Send + Sync>),
    /// A request tried to redirect too many times.
    TooManyRedirects,
    /// An infinite redirect loop was detected.
    RedirectLoop,
    #[doc(hidden)]
    __DontMatchMe,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Http(ref e) => fmt::Display::fmt(e, f),
            Error::Serialize(ref e) => fmt::Display::fmt(e, f),
            Error::TooManyRedirects => f.pad("Too many redirects"),
            Error::RedirectLoop => f.pad("Infinite redirect loop"),
            Error::__DontMatchMe => unreachable!()
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Http(ref e) => e.description(),
            Error::Serialize(ref e) => e.description(),
            Error::TooManyRedirects => "Too many redirects",
            Error::RedirectLoop => "Infinite redirect loop",
            Error::__DontMatchMe => unreachable!()
        }
    }

    fn cause(&self) -> Option<&StdError> {
        match *self {
            Error::Http(ref e) => Some(e),
            Error::Serialize(ref e) => Some(&**e),
            Error::TooManyRedirects |
            Error::RedirectLoop => None,
            Error::__DontMatchMe => unreachable!()
        }
    }
}

fn _assert_types() {
    fn _assert_send<T: Send>() {
    }
    _assert_send::<Error>();
}

impl From<::hyper::Error> for Error {
    fn from(err: ::hyper::Error) -> Error {
        Error::Http(err)
    }
}

impl From<::url::ParseError> for Error {
    fn from(err: ::url::ParseError) -> Error {
        Error::Http(::hyper::Error::Uri(err))
    }
}

impl From<::serde_urlencoded::ser::Error> for Error {
    fn from(err: ::serde_urlencoded::ser::Error) -> Error {
        Error::Serialize(Box::new(err))
    }
}

impl From<::serde_json::Error> for Error {
    fn from(err: ::serde_json::Error) -> Error {
        Error::Serialize(Box::new(err))
    }
}

/// A `Result` alias where the `Err` case is `reqwest::Error`.
pub type Result<T> = ::std::result::Result<T, Error>;