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