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
#[derive(Debug)]
/// Error enum for handling different types of errors within the client
pub enum Error {
/// 400 Bad Request
///
/// The server could not process the request due to invalid request parameters or invalid format
/// of the parameters.
InvalidRequestError,
/// 402 Payment Required
///
/// The request could not be processed because of the user has an insufficient plan. If you want
/// to be able to process this request, get a higher plan.
InsufficientPlan,
/// 403 Forbidden
///
/// The request could not be processed due to invalid API key.
InvalidApiKey,
/// 404 Not Found
///
/// The server could not process the request due to invalid URL or invalid path parameter.
InvalidParameter,
/// 429 Too Many Requests
///
/// The rate limit has been exceeded. Reduce the frequency of requests to avoid this error.
RateLimitError,
/// 500 Internal Server Error
///
/// An unexpected server error has occured.
InternalServerError,
/// Failed to connect with API.
ApiConnectionError,
/// Error from http client.
Reqwest(reqwest::Error),
/// Error from client middleware.
Middleware(reqwest_middleware::Error),
/// Error from JSON creation/processing.
Json(serde_json::Error),
}
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Error {
Error::Reqwest(e)
}
}
impl From<reqwest_middleware::Error> for Error {
fn from(e: reqwest_middleware::Error) -> Error {
Error::Middleware(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Error {
Error::Json(e)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Error::Reqwest(ref e) => Some(e),
Error::Json(ref e) => Some(e),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::InvalidRequestError => {
write!(f, "The server could not process the request due to invalid request parameters or invalid format of the parameters.")
}
Error::InsufficientPlan => {
write!(f, "The request could not be processed because of the user has an insufficient plan. If you want to be able to process this request, get a higher plan.")
}
Error::InvalidApiKey => {
write!(
f,
"The request could not be processed due to invalid API key."
)
}
Error::InvalidParameter => {
write!(f, "The server could not process the request due to invalid URL or invalid path parameter.")
}
Error::RateLimitError => {
write!(f, "The rate limit has been exceeded. Reduce the frequency of requests to avoid this error.")
}
Error::InternalServerError => {
write!(f, "An unexpected server error has occured.")
}
Error::ApiConnectionError => {
write!(f, "Fail to connect to API.")
}
Error::Reqwest(err) => {
write!(f, "{}", err)
}
Error::Middleware(err) => {
write!(f, "{}", err)
}
Error::Json(err) => {
write!(f, "{}", err)
}
}
}
}