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
use reqwest::Error as ReqwestError;
use serde_json::Error as JsonError;
use std::error::Error as StdError;
use std::fmt::Display;
use std::io::Error as IoError;
use std::num::ParseIntError;
pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ErrorCode(pub u64);
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ResponseError {
pub code: ErrorCode,
pub message: String,
}
#[derive(Debug)]
pub enum Error {
Reqwest(ReqwestError),
Json(JsonError),
Io(IoError),
ParseIntError(ParseIntError),
RandomOrg(::reqwest::StatusCode, ResponseError),
Status(::reqwest::StatusCode, String),
Rest(&'static str),
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Error {
Error::ParseIntError(e)
}
}
impl From<::reqwest::Response> for Error {
fn from(mut response: ::reqwest::Response) -> Error {
use std::io::Read;
#[derive(serde::Deserialize)]
pub struct ErrorObject {
error: ResponseError,
}
let status = response.status();
let mut body = String::new();
let _ = response.read_to_string(&mut body);
if !status.is_success() {
if let Ok(e) = ::serde_json::from_str::<ErrorObject>(&body) {
return Error::RandomOrg(status, e.error);
}
}
Error::Status(status, body)
}
}
impl From<IoError> for Error {
fn from(err: IoError) -> Error {
Error::Io(err)
}
}
impl From<ReqwestError> for Error {
fn from(err: ReqwestError) -> Error {
Error::Reqwest(err)
}
}
impl From<JsonError> for Error {
fn from(err: JsonError) -> Error {
Error::Json(err)
}
}
impl Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Error::Reqwest(ref inner) => inner.fmt(f),
Error::Json(ref inner) => inner.fmt(f),
Error::Io(ref inner) => inner.fmt(f),
_ => f.write_str(self.description()),
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::Reqwest(ref inner) => inner.description(),
Error::Json(ref inner) => inner.description(),
Error::Io(ref inner) => inner.description(),
Error::ParseIntError(ref inner) => inner.description(),
Error::Rest(msg) => msg,
Error::RandomOrg(status, _) | Error::Status(status, _) => status
.canonical_reason()
.unwrap_or("Unknown bad HTTP status"),
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
Error::Reqwest(ref inner) => Some(inner),
Error::Json(ref inner) => Some(inner),
Error::Io(ref inner) => Some(inner),
Error::ParseIntError(ref inner) => Some(inner),
_ => None,
}
}
}