coinpaprika_api/
error.rs

1#[derive(Debug)]
2/// Error enum for handling different types of errors within the client
3pub enum Error {
4    /// 400 Bad Request
5    ///
6    /// The server could not process the request due to invalid request parameters or invalid format
7    /// of the parameters.
8    InvalidRequestError,
9
10    /// 402 Payment Required
11    ///
12    /// The request could not be processed because of the user has an insufficient plan. If you want
13    /// to be able to process this request, get a higher plan.
14    InsufficientPlan,
15
16    /// 403 Forbidden
17    ///
18    /// The request could not be processed due to invalid API key.
19    InvalidApiKey,
20
21    /// 404 Not Found
22    ///
23    /// The server could not process the request due to invalid URL or invalid path parameter.
24    InvalidParameter,
25
26    /// 429 Too Many Requests
27    ///
28    /// The rate limit has been exceeded. Reduce the frequency of requests to avoid this error.
29    RateLimitError,
30
31    /// 500 Internal Server Error
32    ///
33    /// An unexpected server error has occured.
34    InternalServerError,
35
36    /// Failed to connect with API.
37    ApiConnectionError,
38
39    /// Error from http client.
40    Reqwest(reqwest::Error),
41
42    /// Error from client middleware.
43    Middleware(reqwest_middleware::Error),
44
45    /// Error from JSON creation/processing.
46    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}