nautilus_binance/spot/http/
error.rs1use std::fmt::Display;
19
20use nautilus_network::http::error::HttpClientError;
21
22pub use crate::common::sbe::SbeDecodeError;
24
25#[derive(Debug)]
27pub enum BinanceSpotHttpError {
28 MissingCredentials,
30 BinanceError {
32 code: i64,
34 message: String,
36 },
37 SbeDecodeError(SbeDecodeError),
39 JsonError(String),
41 ValidationError(String),
43 NetworkError(String),
45 Timeout(String),
47 Canceled(String),
49 UnexpectedStatus {
51 status: u16,
53 body: String,
55 },
56}
57
58impl Display for BinanceSpotHttpError {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 match self {
61 Self::MissingCredentials => write!(f, "Missing API credentials"),
62 Self::BinanceError { code, message } => {
63 write!(f, "Binance error {code}: {message}")
64 }
65 Self::SbeDecodeError(err) => write!(f, "SBE decode error: {err}"),
66 Self::JsonError(msg) => write!(f, "JSON decode error: {msg}"),
67 Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
68 Self::NetworkError(msg) => write!(f, "Network error: {msg}"),
69 Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
70 Self::Canceled(msg) => write!(f, "Canceled: {msg}"),
71 Self::UnexpectedStatus { status, body } => {
72 write!(f, "Unexpected status {status}: {body}")
73 }
74 }
75 }
76}
77
78impl std::error::Error for BinanceSpotHttpError {}
79
80impl From<SbeDecodeError> for BinanceSpotHttpError {
81 fn from(err: SbeDecodeError) -> Self {
82 Self::SbeDecodeError(err)
83 }
84}
85
86impl From<anyhow::Error> for BinanceSpotHttpError {
87 fn from(err: anyhow::Error) -> Self {
88 Self::NetworkError(err.to_string())
89 }
90}
91
92impl From<HttpClientError> for BinanceSpotHttpError {
93 fn from(err: HttpClientError) -> Self {
94 match err {
95 HttpClientError::TimeoutError(msg) => Self::Timeout(msg),
96 HttpClientError::InvalidProxy(msg) | HttpClientError::ClientBuildError(msg) => {
97 Self::NetworkError(msg)
98 }
99 HttpClientError::Error(msg) => Self::NetworkError(msg),
100 }
101 }
102}
103
104pub type BinanceSpotHttpResult<T> = Result<T, BinanceSpotHttpError>;