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
use std::error::Error;
use std::fmt::{Display, Result as FmtResult, Formatter};
use hyper::status::StatusCode;
use hyper;
use serde_json;
#[derive(Debug)]
pub enum APIError {
ExhaustedListing,
HTTPError(StatusCode),
HyperError(hyper::Error),
JSONError(serde_json::Error),
}
impl Display for APIError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "Error! {}. ({:?})", self.description(), self)
}
}
impl Error for APIError {
fn description(&self) -> &str {
match *self {
APIError::HTTPError(_) => "The API returned a non-success error code",
APIError::HyperError(_) => "An error occurred while processing the HTTP response",
APIError::JSONError(_) => {
"The JSON sent by Reddit did not match what rawr was expecting"
}
_ => "This error should not have occurred. Please file a bug",
}
}
}
impl From<hyper::Error> for APIError {
fn from(err: hyper::Error) -> APIError {
APIError::HyperError(err)
}
}
impl From<serde_json::Error> for APIError {
fn from(err: serde_json::Error) -> APIError {
APIError::JSONError(err)
}
}