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
//! Client errors

use std::io::Error as IoError;
use std::time::Duration;

use hyper::Error as HttpError;
use hyper::StatusCode;
use hyper::error::UriError;
use serde_json::error::Error as SerdeError;

error_chain! {
    errors {
        #[doc = "Client side error returned for faulty requests"]
        Fault {
            code: StatusCode,
            error: ClientError,
        } {
            display("{}: '{}'", code, error.message)
            description(error.message.as_str())
          }
        #[doc = "Error kind returned when a credential's rate limit has been exhausted. Wait for the reset duration before issuing more requests"]
        RateLimit {
            reset: Duration
        } {
            display("Rate limit exhausted. Will reset in {} seconds", reset.as_secs())
        }
    }
    foreign_links {
        Codec(SerdeError);
        Http(HttpError);
        IO(IoError);
        URI(UriError);
    }
}

// representations

#[derive(Debug, Deserialize, PartialEq)]
pub struct FieldErr {
    pub resource: String,
    pub field: Option<String>,
    pub code: String,
    pub message: Option<String>,
    pub documentation_url: Option<String>,
}

#[derive(Debug, Deserialize, PartialEq)]
pub struct ClientError {
    pub message: String,
    pub errors: Option<Vec<FieldErr>>,
}

#[cfg(test)]
mod tests {
    use serde_json;
    use super::{ClientError, FieldErr};
    #[test]
    fn deserialize_client_field_errors() {
        for (json, expect) in vec![
            // see https://github.com/softprops/hubcaps/issues/31
            (
                r#"{"message": "Validation Failed","errors":
                [{
                    "resource": "Release",
                    "code": "custom",
                    "message": "Published releases must have a valid tag"
                }]}"#,
                ClientError {
                    message: "Validation Failed".to_owned(),
                    errors: Some(vec![
                        FieldErr {
                            resource: "Release".to_owned(),
                            code: "custom".to_owned(),
                            field: None,
                            message: Some(
                                "Published releases \
                                 must have a valid tag"
                                    .to_owned(),
                            ),
                            documentation_url: None,
                        },
                    ]),
                },
            ),
        ] {
            assert_eq!(serde_json::from_str::<ClientError>(json).unwrap(), expect);
        }
    }
}