aboutsummaryrefslogtreecommitdiff
path: root/src/http/error.rs
blob: f185675db70a41a70f16363dc833980ff4a910a0 (plain) (blame)
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
use futures::Canceled;
use hyper::{
    error::{Error as HyperError, UriError},
    Response,
};
use native_tls::Error as TlsError;
use serde_json::Error as JsonError;
use std::{
    cell::BorrowMutError,
    error::Error as StdError,
    fmt::{Display, Error as FmtError, Formatter, Result as FmtResult},
    io::Error as IoError,
    result::Result as StdResult,
};
use super::ratelimiting::RateLimitError;
use tokio_timer::TimerError;

pub type Result<T> = StdResult<T, Error>;

#[derive(Debug)]
pub enum Error {
    /// There was an error mutably borrowing an `std::cell::RefCell`.
    BorrowMut(BorrowMutError),
    /// A future was canceled.
    ///
    /// This most likely occurred during a pre-emptive ratelimit.
    Canceled(Canceled),
    /// An error from the `std::fmt` module.
    Format(FmtError),
    /// An error from the `hyper` crate.
    Hyper(HyperError),
    /// When a status code was unexpectedly received for a request's status.
    InvalidRequest(Response),
    /// An error from the `std::io` module.
    Io(IoError),
    /// An error from the `serde_json` crate.
    Json(JsonError),
    /// An error from the `ratelimiting` module.
    RateLimit(RateLimitError),
    /// An error occurred while creating a timer.
    Timer(TimerError),
    /// An error from the `native_tls` crate.
    Tls(TlsError),
    /// When a status is received, but the verification to ensure the response
    /// is valid does not recognize the status.
    UnknownStatus(u16),
    /// A `hyper` error while parsing a Uri.
    Uri(UriError),
}

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::BorrowMut(ref inner) => inner.description(),
            Error::Canceled(ref inner) => inner.description(),
            Error::Format(ref inner) => inner.description(),
            Error::Hyper(ref inner) => inner.description(),
            Error::InvalidRequest(_) => "Received an unexpected status code",
            Error::Io(ref inner) => inner.description(),
            Error::Json(ref inner) => inner.description(),
            Error::RateLimit(ref inner) => inner.description(),
            Error::Timer(ref inner) => inner.description(),
            Error::Tls(ref inner) => inner.description(),
            Error::UnknownStatus(_) => "Verification does not understand status",
            Error::Uri(ref inner) => inner.description(),
        }
    }
}

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

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

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

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

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

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

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

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

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