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
use serde::Deserialize;
use std::fmt;
#[derive(Debug)]
pub enum Error<TE> {
TransportError(TE),
BadStatusCodeError(http::StatusCode),
BadContentTypeError(Option<http::header::HeaderValue>),
UnparseableResponseError(UnparseableResponseError),
ApiError(ApiError),
UnsupportedFeature,
Other(&'static str),
}
impl<TE: std::error::Error> std::error::Error for Error<TE> {}
impl<TE: fmt::Display> fmt::Display for Error<TE> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::TransportError(te) => write!(f, "transport error: {}", te),
Error::BadStatusCodeError(sc) => write!(f, "bad status code: {}", sc),
Error::BadContentTypeError(ct) => write!(f, "bad content type: got {:?}", ct),
Error::UnparseableResponseError(e) => write!(f, "unparseable response: {:?}", e),
Error::ApiError(e) => write!(f, "JSON API error: {:?}", e),
Error::UnsupportedFeature => write!(f, "this device does not support that feature"),
Error::Other(e) => write!(f, "error: {}", e),
}
}
}
impl<TE> From<serde_json::Error> for Error<TE> {
fn from(e: serde_json::Error) -> Self {
Error::UnparseableResponseError(UnparseableResponseError::JsonDeError(e))
}
}
impl<TE> From<quick_xml::DeError> for Error<TE> {
fn from(e: quick_xml::DeError) -> Self {
Error::UnparseableResponseError(UnparseableResponseError::XmlDeError(e))
}
}
impl<TE> From<ApiError> for Error<TE> {
fn from(e: ApiError) -> Self {
Error::ApiError(e)
}
}
#[derive(Debug)]
pub enum UnparseableResponseError {
JsonDeError(serde_json::Error),
XmlDeError(quick_xml::DeError),
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ApiError {
InvalidParameter,
AccessForbidden,
UnsupportedHttpMethod,
UnsupportedApiVersion,
UnsupportedApiMethod,
InvalidJsonFormat,
RequiredParameterIsMissing,
InternalError,
OtherError(Box<RawJsonApiError>),
}
impl From<RawJsonApiError> for ApiError {
fn from(e: RawJsonApiError) -> Self {
match e.code {
1000 => ApiError::InvalidParameter,
2001 => ApiError::AccessForbidden,
2002 => ApiError::UnsupportedHttpMethod,
2003 => ApiError::UnsupportedApiVersion,
2004 => ApiError::UnsupportedApiMethod,
4000 => ApiError::InvalidJsonFormat,
4002 => ApiError::RequiredParameterIsMissing,
8000 => ApiError::InternalError,
_ => ApiError::OtherError(Box::new(e)),
}
}
}
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RawJsonApiError {
pub code: u32,
pub message: Option<String>,
}
impl<TE> From<RawJsonApiError> for Error<TE> {
fn from(e: RawJsonApiError) -> Self {
Error::ApiError(e.into())
}
}
pub(crate) trait ResultExt {
fn map_404_to_unsupported_feature(self) -> Self;
}
impl<T, TE> ResultExt for std::result::Result<T, Error<TE>> {
fn map_404_to_unsupported_feature(self) -> Self {
match self {
Err(Error::BadStatusCodeError(http::StatusCode::NOT_FOUND)) => {
Err(Error::UnsupportedFeature)
}
other => other,
}
}
}