nautilus_binance/spot/http/
error.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Binance Spot HTTP error types.
17
18use std::fmt::Display;
19
20use nautilus_network::http::error::HttpClientError;
21
22// Re-export unified SBE decode error
23pub use crate::common::sbe::SbeDecodeError;
24
25/// Binance Spot HTTP client error type.
26#[derive(Debug)]
27pub enum BinanceSpotHttpError {
28    /// Missing API credentials for authenticated request.
29    MissingCredentials,
30    /// Binance API returned an error response.
31    BinanceError {
32        /// Binance error code.
33        code: i64,
34        /// Error message from Binance.
35        message: String,
36    },
37    /// SBE decode error.
38    SbeDecodeError(SbeDecodeError),
39    /// Request validation error.
40    ValidationError(String),
41    /// Network or connection error.
42    NetworkError(String),
43    /// Request timed out.
44    Timeout(String),
45    /// Request was canceled.
46    Canceled(String),
47    /// Unexpected HTTP status code.
48    UnexpectedStatus {
49        /// HTTP status code.
50        status: u16,
51        /// Response body (hex encoded for SBE).
52        body: String,
53    },
54}
55
56impl Display for BinanceSpotHttpError {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Self::MissingCredentials => write!(f, "Missing API credentials"),
60            Self::BinanceError { code, message } => {
61                write!(f, "Binance error {code}: {message}")
62            }
63            Self::SbeDecodeError(err) => write!(f, "SBE decode error: {err}"),
64            Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
65            Self::NetworkError(msg) => write!(f, "Network error: {msg}"),
66            Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
67            Self::Canceled(msg) => write!(f, "Canceled: {msg}"),
68            Self::UnexpectedStatus { status, body } => {
69                write!(f, "Unexpected status {status}: {body}")
70            }
71        }
72    }
73}
74
75impl std::error::Error for BinanceSpotHttpError {}
76
77impl From<SbeDecodeError> for BinanceSpotHttpError {
78    fn from(err: SbeDecodeError) -> Self {
79        Self::SbeDecodeError(err)
80    }
81}
82
83impl From<anyhow::Error> for BinanceSpotHttpError {
84    fn from(err: anyhow::Error) -> Self {
85        Self::NetworkError(err.to_string())
86    }
87}
88
89impl From<HttpClientError> for BinanceSpotHttpError {
90    fn from(err: HttpClientError) -> Self {
91        match err {
92            HttpClientError::TimeoutError(msg) => Self::Timeout(msg),
93            HttpClientError::InvalidProxy(msg) | HttpClientError::ClientBuildError(msg) => {
94                Self::NetworkError(msg)
95            }
96            HttpClientError::Error(msg) => Self::NetworkError(msg),
97        }
98    }
99}
100
101/// Result type for Binance Spot HTTP operations.
102pub type BinanceSpotHttpResult<T> = Result<T, BinanceSpotHttpError>;