1#[derive(Debug)]
2pub enum Error {
4 InvalidRequestError,
9
10 InsufficientPlan,
15
16 InvalidApiKey,
20
21 InvalidParameter,
25
26 RateLimitError,
30
31 InternalServerError,
35
36 ApiConnectionError,
38
39 Reqwest(reqwest::Error),
41
42 Middleware(reqwest_middleware::Error),
44
45 Json(serde_json::Error),
47}
48
49impl From<reqwest::Error> for Error {
50 fn from(e: reqwest::Error) -> Error {
51 Error::Reqwest(e)
52 }
53}
54
55impl From<reqwest_middleware::Error> for Error {
56 fn from(e: reqwest_middleware::Error) -> Error {
57 Error::Middleware(e)
58 }
59}
60
61impl From<serde_json::Error> for Error {
62 fn from(e: serde_json::Error) -> Error {
63 Error::Json(e)
64 }
65}
66
67impl std::error::Error for Error {
68 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69 match *self {
70 Error::Reqwest(ref e) => Some(e),
71 Error::Json(ref e) => Some(e),
72 _ => None,
73 }
74 }
75}
76
77impl std::fmt::Display for Error {
78 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
79 match self {
80 Error::InvalidRequestError => {
81 write!(f, "The server could not process the request due to invalid request parameters or invalid format of the parameters.")
82 }
83 Error::InsufficientPlan => {
84 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.")
85 }
86 Error::InvalidApiKey => {
87 write!(
88 f,
89 "The request could not be processed due to invalid API key."
90 )
91 }
92 Error::InvalidParameter => {
93 write!(f, "The server could not process the request due to invalid URL or invalid path parameter.")
94 }
95 Error::RateLimitError => {
96 write!(f, "The rate limit has been exceeded. Reduce the frequency of requests to avoid this error.")
97 }
98 Error::InternalServerError => {
99 write!(f, "An unexpected server error has occured.")
100 }
101 Error::ApiConnectionError => {
102 write!(f, "Fail to connect to API.")
103 }
104 Error::Reqwest(err) => {
105 write!(f, "{}", err)
106 }
107 Error::Middleware(err) => {
108 write!(f, "{}", err)
109 }
110 Error::Json(err) => {
111 write!(f, "{}", err)
112 }
113 }
114 }
115}