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
use crate::resource::ErrorObject;
use failure::{self, format_err, Fail};
use http::{self, StatusCode};
use serde_json;
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
inner: Box<ErrorKind>,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
#[derive(Debug, Fail)]
enum ErrorKind {
#[fail(display = "Deserialize error: {}", 0)]
DeserializeError(failure::Error),
#[fail(display = "HTTP error: {}", 0)]
HttpError(http::Error),
#[fail(display = "HTTP error: {}", 0)]
UrlParseError(url::ParseError),
#[fail(display = "Api request failed with {}: {:?}", status, error)]
ErrorResponse {
status: StatusCode,
error: ErrorObject,
},
#[cfg(feature = "reqwest")]
#[fail(display = "Request error: {}", 0)]
RequestError(reqwest::Error),
}
impl Error {
pub(crate) fn from_error_response(status: StatusCode, error: ErrorObject) -> Self {
Self {
inner: Box::new(ErrorKind::ErrorResponse { status, error }),
}
}
pub(crate) fn unexpected_response(reason: &'static str) -> Self {
Self {
inner: Box::new(ErrorKind::DeserializeError(format_err!("{}", reason))),
}
}
pub fn error_response(&self) -> Option<&ErrorObject> {
match &*self.inner {
ErrorKind::HttpError(_)
| ErrorKind::DeserializeError(_)
| ErrorKind::UrlParseError(_) => None,
ErrorKind::ErrorResponse { error, .. } => Some(error),
#[cfg(feature = "reqwest")]
ErrorKind::RequestError(_) => None,
}
}
pub fn status_code(&self) -> Option<StatusCode> {
match &*self.inner {
ErrorKind::DeserializeError(_)
| ErrorKind::HttpError(_)
| ErrorKind::UrlParseError(_) => None,
ErrorKind::ErrorResponse { status, .. } => Some(*status),
#[cfg(feature = "reqwest")]
ErrorKind::RequestError(source) => source.status(),
}
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Self {
inner: Box::new(ErrorKind::DeserializeError(e.into())),
}
}
}
impl From<http::Error> for Error {
fn from(e: http::Error) -> Self {
Self {
inner: Box::new(ErrorKind::HttpError(e.into())),
}
}
}
impl From<url::ParseError> for Error {
fn from(source: url::ParseError) -> Self {
Self {
inner: Box::new(ErrorKind::UrlParseError(source)),
}
}
}
#[cfg(feature = "reqwest")]
impl From<reqwest::Error> for Error {
fn from(source: reqwest::Error) -> Self {
Self {
inner: Box::new(ErrorKind::RequestError(source)),
}
}
}