alphavantage/
error.rs

1/// Set of errors which can occur when calling the API.
2#[derive(Debug)]
3pub enum Error {
4    /// Error establishing a network connection.
5    ConnectionError(String),
6    /// HTTP error returned by the API.
7    ServerError(u16),
8    /// Error parsing the API response.
9    ParsingError(String),
10    /// Error returned by the API.
11    APIError(String),
12}
13
14impl std::fmt::Display for Error {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            Error::ConnectionError(e) => write!(f, "connection error: {}", e),
18            Error::ServerError(e) => write!(f, "server returned HTTP status code {}", e),
19            Error::ParsingError(e) => write!(f, "parsing error: {}", e),
20            Error::APIError(e) => write!(f, "API error: {}", e),
21        }
22    }
23}
24
25impl std::error::Error for Error {}
26
27impl From<reqwest::Error> for Error {
28    fn from(inner: reqwest::Error) -> Error {
29        Error::ConnectionError(inner.to_string())
30    }
31}
32
33impl From<serde_json::Error> for Error {
34    fn from(inner: serde_json::Error) -> Error {
35        Error::ParsingError(inner.to_string())
36    }
37}
38
39impl From<chrono::format::ParseError> for Error {
40    fn from(inner: chrono::format::ParseError) -> Error {
41        Error::ParsingError(inner.to_string())
42    }
43}