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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use url::ParseError;
use serde_json::error as serdeErr;
use hyper::error as hyperErr;
use std::error;
use std::io;
use std::fmt;
use std::result;
use std::string;
use std::str::Utf8Error;
pub type Result<T> = result::Result<T, GithubError>;
#[derive(Debug)]
pub enum GithubError {
JsonParsingSyntax(serdeErr::ErrorCode, usize, usize),
JsonParsingIo(io::Error),
JsonParsingFromUtf8(string::FromUtf8Error),
Request,
RequestMethod,
RequestUri(ParseError),
RequestVersion,
RequestHeader,
RequestTooLarge,
RequestStatus,
RequestIo(io::Error),
RequestSsl(Box<error::Error + Send + Sync>),
RequestUtf8(Utf8Error),
NonJsonBody,
InvalidFields,
Unauthorized,
QueryLimit,
LibIo(io::Error),
Github404,
Mime,
LibError,
}
impl fmt::Display for GithubError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
_ => write!(f, "{}", self),
}
}
}
impl error::Error for GithubError {
fn description(&self) -> &str {
use self::GithubError::*;
match *self {
JsonParsingSyntax(..) => "Syntax Error",
JsonParsingIo(ref err) | RequestIo(ref err) => error::Error::description(err),
JsonParsingFromUtf8(..) => "UTF-8 Error",
RequestMethod => "Method in Headers was malformed",
RequestUri(ref err) => error::Error::description(err),
RequestVersion => "Request protocol in Headers was malformed",
RequestHeader => "Headers were malformed",
RequestTooLarge => "Request payload is too big to work",
RequestStatus => "Status Code returned was not a valid code",
RequestSsl(..) => "While performing SSL actions with the request an error occurred",
RequestUtf8(ref err) => error::Error::description(err),
Request => "Something went wrong while connecting. We are unsure what the problem is.",
LibIo(..) => "Error occured while performing internal library Io Actions",
Github404 => "Message: Not Found, Documentation URL: https://developer.github.com/v3",
NonJsonBody => "JSON was not returned",
InvalidFields => "You've made a request with invalid fields",
Unauthorized => "Your request was unauthorized. Check your OAuth Token.",
QueryLimit => "You've hit your query limit to the API.",
Mime => "While making a Mime type for the Header an error occurred",
LibError => {
"The github-rs lib has caused an error. Please file a bug report with your \
request, what you expected, and what method you used that failed."
}
}
}
}
impl From<serdeErr::Error> for GithubError {
fn from(serde: serdeErr::Error) -> Self {
use self::GithubError::*;
use serde_json::error::Error::*;
match serde {
Syntax(x, y, z) => JsonParsingSyntax(x, y, z),
Io(err) => JsonParsingIo(err),
}
}
}
impl From<hyperErr::Error> for GithubError {
fn from(hyper: hyperErr::Error) -> Self {
use self::GithubError::*;
use hyper::error::Error::*;
match hyper {
Method => RequestMethod,
Uri(err) => RequestUri(err),
Version => RequestVersion,
Header => RequestHeader,
TooLarge => RequestTooLarge,
Status => RequestStatus,
Io(err) => RequestIo(err),
Ssl(err) => RequestSsl(err),
Utf8(err) => RequestUtf8(err),
_ => Request,
}
}
}
macro_rules! try_serde {
($x:expr) => (
match $x {
Ok(x) => Ok(x),
Err(err) => Err(GithubError::from(err)),
}
);
}
macro_rules! try_hyper {
($x:expr) => (
match $x {
Ok(x) => x,
Err(err) => return Err(GithubError::from(err)),
}
);
}
macro_rules! try_mime {
($x:expr) => (
match $x {
Ok(x) => x,
Err(_) => return Err(GithubError::Mime),
}
);
}