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

use std::error::Error as StdError;
use std::io::Error as IoError;
use hyper::Error as HttpError;
use hyper::status::StatusCode;
use serde_json::error::Error as SerdeError;

// todo: look into error_chain crate to remove boiler plate

/// enumerated types of client errors
error_chain! {
    errors {
        Fault {
            code: StatusCode,
            error: ClientError,
        }
    }
    foreign_links {
        Codec(SerdeError);
        Http(HttpError);
        IO(IoError);
    }
}

// preresentations

#[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);
        }
    }
}