nautilus_binance/
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 adapter error types.
17
18use std::fmt;
19
20use crate::{http::error::BinanceHttpError, websocket::error::BinanceWsError};
21
22/// Top-level Binance adapter error type.
23#[derive(Debug)]
24pub enum BinanceError {
25    /// HTTP client error.
26    Http(BinanceHttpError),
27    /// WebSocket client error.
28    WebSocket(BinanceWsError),
29    /// Configuration error.
30    Config(String),
31    /// Data parsing error.
32    Parse(String),
33}
34
35impl fmt::Display for BinanceError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::Http(e) => write!(f, "HTTP error: {e}"),
39            Self::WebSocket(e) => write!(f, "WebSocket error: {e}"),
40            Self::Config(msg) => write!(f, "Configuration error: {msg}"),
41            Self::Parse(msg) => write!(f, "Parse error: {msg}"),
42        }
43    }
44}
45
46impl std::error::Error for BinanceError {
47    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48        match self {
49            Self::Http(e) => Some(e),
50            Self::WebSocket(e) => Some(e),
51            Self::Config(_) | Self::Parse(_) => None,
52        }
53    }
54}
55
56impl From<BinanceHttpError> for BinanceError {
57    fn from(err: BinanceHttpError) -> Self {
58        Self::Http(err)
59    }
60}
61
62impl From<BinanceWsError> for BinanceError {
63    fn from(err: BinanceWsError) -> Self {
64        Self::WebSocket(err)
65    }
66}
67
68/// Result type for Binance adapter operations.
69pub type BinanceResult<T> = Result<T, BinanceError>;