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
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Error {
    pub code: i32,
    pub message: String
}

#[derive(Clone, Debug)]
pub struct ErrorResponse {
    pub status: ::hyper::status::StatusCode,
    pub errors: Option<Vec<Error>>,
    pub raw_response: String,
    pub rate_limit: Option<RateLimitStatus>
}

impl std::error::Error for ErrorResponse {
    fn description(&self) -> &str {
        "the server returned an error response"
    }
}

impl std::fmt::Display for ErrorResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self.errors {
            Some(ref x) => {
                let ref e = x[0];
                write!(f, "{}: {} {}", self.status, e.code, e.message)
            },
            None => write!(f, "{}: {}", self.status, self.raw_response)
        }
    }
}