Skip to main content

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::spot::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    /// JSON decode error.
40    JsonError(String),
41    /// Response parse error after a venue response was received.
42    ResponseParseError(String),
43    /// Request validation error.
44    ValidationError(String),
45    /// Network or connection error.
46    NetworkError(String),
47    /// Request timed out.
48    Timeout(String),
49    /// Request was canceled.
50    Canceled(String),
51    /// Unexpected HTTP status code.
52    UnexpectedStatus {
53        /// HTTP status code.
54        status: u16,
55        /// Response body (hex encoded for SBE).
56        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
107/// Result type for Binance Spot HTTP operations.
108pub type BinanceSpotHttpResult<T> = Result<T, BinanceSpotHttpError>;