nautilus_binance/http/
error.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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 HTTP error types.
17
18use std::fmt::{self, Display};
19
20use nautilus_network::http::error::HttpClientError;
21
22/// Binance HTTP client error type.
23#[derive(Debug)]
24pub enum BinanceHttpError {
25    /// Missing API credentials for authenticated request.
26    MissingCredentials,
27    /// Binance API returned an error response.
28    BinanceError {
29        /// Binance error code.
30        code: i64,
31        /// Error message from Binance.
32        message: String,
33    },
34    /// JSON parsing or serialization error.
35    JsonError(String),
36    /// Request validation error.
37    ValidationError(String),
38    /// Network or connection error.
39    NetworkError(String),
40    /// Request timed out.
41    Timeout(String),
42    /// Request was canceled.
43    Canceled(String),
44    /// Unexpected HTTP status code.
45    UnexpectedStatus {
46        /// HTTP status code.
47        status: u16,
48        /// Response body.
49        body: String,
50    },
51}
52
53impl Display for BinanceHttpError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            Self::MissingCredentials => write!(f, "Missing API credentials"),
57            Self::BinanceError { code, message } => {
58                write!(f, "Binance error {code}: {message}")
59            }
60            Self::JsonError(msg) => write!(f, "JSON error: {msg}"),
61            Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
62            Self::NetworkError(msg) => write!(f, "Network error: {msg}"),
63            Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
64            Self::Canceled(msg) => write!(f, "Canceled: {msg}"),
65            Self::UnexpectedStatus { status, body } => {
66                write!(f, "Unexpected status {status}: {body}")
67            }
68        }
69    }
70}
71
72impl std::error::Error for BinanceHttpError {}
73
74impl From<serde_json::Error> for BinanceHttpError {
75    fn from(err: serde_json::Error) -> Self {
76        Self::JsonError(err.to_string())
77    }
78}
79
80impl From<anyhow::Error> for BinanceHttpError {
81    fn from(err: anyhow::Error) -> Self {
82        Self::NetworkError(err.to_string())
83    }
84}
85
86impl From<HttpClientError> for BinanceHttpError {
87    fn from(err: HttpClientError) -> Self {
88        match err {
89            HttpClientError::TimeoutError(msg) => Self::Timeout(msg),
90            HttpClientError::InvalidProxy(msg) | HttpClientError::ClientBuildError(msg) => {
91                Self::NetworkError(msg)
92            }
93            HttpClientError::Error(msg) => Self::NetworkError(msg),
94        }
95    }
96}
97
98/// Result type for Binance HTTP operations.
99pub type BinanceHttpResult<T> = Result<T, BinanceHttpError>;