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
use std::{fmt, str};
use std::fmt::Display;
use json;
use reqwest;
error_chain! {
errors {
Api(err: ApiError) {
description("API error")
display("API error - {}", err.message)
}
Http(context: String) {
description("HTTP error")
display("HTTP error {}", context)
}
InvalidBaseUrl {
description("invalid base URL")
}
InvalidDataUri(uri: String) {
description("invalid data URI")
display("invalid data URI '{}'", uri)
}
InvalidAlgoUri(uri: String) {
description("invalid algorithm URI")
display("invalid algorithm URI: {}", &uri)
}
DecodeJson(item: &'static str) {
description("json decode error")
display("failed to decode {} json", item)
}
EncodeJson(item: &'static str) {
description("json encode error")
display("failed to encode {} as json", item)
}
DecodeBase64(item: &'static str) {
description("base64 error")
display("failed to decode {} as base64", item)
}
Io(context: String) {
description("I/O error")
display("I/O error {}", context)
}
InvalidContentType(t: String) {
description("invalid content type")
display("invalid content type: '{}'", t)
}
MismatchedContentType(expected: &'static str) {
description("mismatched content type")
display("content did not match content type: '{}'", expected)
}
UnexpectedContentType(expected: &'static str, actual: String) {
description("unexpected content type")
display("expected content type '{}', received '{}'", expected, actual)
}
NotFound(url: reqwest::Url) {
description("404 Not Found")
display("404 Not Found ({})", url)
}
MissingDataType {
description("API response missing data type")
}
InvalidDataType(t: String) {
description("invalid data type")
display("API responded with invalid data type: '{}'", t)
}
UnexpectedDataType(expected: &'static str, actual: String) {
description("unexpected data type")
display("expected API response with data type '{}', received '{}'", expected, actual)
}
UnsupportedInput {
description("unsupported input type")
}
#[doc(hidden)]
__DontMatchMe {}
}
}
#[cfg_attr(feature="with-serde", derive(Deserialize))]
#[cfg_attr(feature="with-rustc-serialize", derive(RustcDecodable))]
#[derive(Debug)]
pub struct ApiError {
pub message: String,
pub stacktrace: Option<String>,
}
impl Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.stacktrace {
Some(ref trace) => write!(f, "{}\n{}", self.message, trace),
None => write!(f, "{}", self.message),
}
}
}
#[cfg_attr(feature="with-serde", derive(Deserialize))]
#[cfg_attr(feature="with-rustc-serialize", derive(RustcDecodable))]
#[derive(Debug)]
#[doc(hidden)]
pub struct ApiErrorResponse {
pub error: ApiError,
}
#[doc(hidden)]
pub fn decode(json_str: &str) -> Error {
let decoded_error = json::decode_str::<ApiErrorResponse>(json_str);
match decoded_error.chain_err(|| ErrorKind::DecodeJson("api error response")) {
Ok(err_res) => ErrorKind::Api(err_res.error).into(),
Err(err) => err,
}
}