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
use std::io::Error as IoError;
use std::time::Duration;
use error_chain::*;
use http::StatusCode;
use reqwest::Error as ReqwestError;
use serde::Deserialize;
use serde_json::error::Error as SerdeError;
use url::ParseError;
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);
Reqwest(ReqwestError);
Url(ParseError);
IO(IoError);
}
}
#[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>>,
pub documentation_url: Option<String>,
}
#[cfg(test)]
mod tests {
use super::{ClientError, FieldErr};
use serde_json;
#[test]
fn deserialize_client_field_errors() {
for (json, expect) in vec![
(
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,
}]),
documentation_url: None,
},
),
] {
assert_eq!(serde_json::from_str::<ClientError>(json).unwrap(), expect);
}
}
#[test]
fn deserialize_client_top_level_documentation_url() {
let json = serde_json::json!({
"message": "Not Found",
"documentation_url": "https://developer.github.com/v3/activity/watching/#set-a-repository-subscription"
});
let expect = ClientError {
message: String::from("Not Found"),
errors: None,
documentation_url: Some(String::from(
"https://developer.github.com/v3/activity/watching/#set-a-repository-subscription",
)),
};
assert_eq!(serde_json::from_value::<ClientError>(json).unwrap(), expect)
}
}