exc_binance/
error.rs

1use anyhow::anyhow;
2use exc_core::ExchangeError;
3use thiserror::Error;
4
5use crate::{http::error::RestError, websocket::error::WsError};
6
7/// All errors in [`exc-binance`]
8#[derive(Debug, Error)]
9pub enum Error {
10    /// Rest API errors.
11    #[error("rest: {0}")]
12    Rest(#[from] RestError),
13    /// Websocket API errors.
14    #[error("websocket: {0}")]
15    Ws(#[from] WsError),
16    /// All other errors.
17    #[error("unknown: {0}")]
18    Unknown(#[from] anyhow::Error),
19    /// Wrong response type.
20    #[error("wrong response type")]
21    WrongResponseType,
22}
23
24impl From<Error> for ExchangeError {
25    fn from(err: Error) -> Self {
26        match err {
27            Error::Unknown(err) => Self::Other(err),
28            Error::WrongResponseType => Self::Other(anyhow!("wrong response type")),
29            Error::Rest(err) => match err {
30                RestError::Http(_) | RestError::Hyper(_) => Self::Unavailable(err.into()),
31                RestError::Exchange(err) => err,
32                _ => Self::Other(err.into()),
33            },
34            Error::Ws(err) => match &err {
35                WsError::ListenKeyExpired(_)
36                | WsError::StreamSubscribed(_)
37                | WsError::TokioTower(_)
38                | WsError::TransportIsBoken
39                | WsError::TransportTimeout
40                | WsError::UnknownConnection(_)
41                | WsError::Websocket(_) => Self::Unavailable(err.into()),
42                _ => Self::Other(err.into()),
43            },
44        }
45    }
46}