Skip to main content

alpaca_data/
error.rs

1use alpaca_http::ErrorMeta;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum Error {
6    #[error("invalid configuration: {0}")]
7    InvalidConfiguration(String),
8    #[error("missing credentials")]
9    MissingCredentials,
10    #[error("invalid request: {0}")]
11    InvalidRequest(String),
12    #[error(transparent)]
13    Http(#[from] alpaca_http::Error),
14}
15
16impl Error {
17    #[must_use]
18    pub fn meta(&self) -> Option<&ErrorMeta> {
19        match self {
20            Self::Http(error) => error.meta(),
21            Self::InvalidConfiguration(_) | Self::MissingCredentials | Self::InvalidRequest(_) => {
22                None
23            }
24        }
25    }
26}
27
28impl From<alpaca_core::Error> for Error {
29    fn from(error: alpaca_core::Error) -> Self {
30        match error {
31            alpaca_core::Error::InvalidConfiguration(message) => {
32                Self::InvalidConfiguration(message)
33            }
34            alpaca_core::Error::InvalidRequest(message) => Self::InvalidRequest(message),
35        }
36    }
37}