nautilus_binance/spot/websocket/trading/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 WebSocket API error types.
17
18use std::fmt::Display;
19
20use crate::common::sbe::error::SbeDecodeError;
21
22/// Binance WebSocket API client error type.
23#[derive(Debug)]
24pub enum BinanceWsApiError {
25 /// General client error.
26 ClientError(String),
27 /// Handler not available (channel closed).
28 HandlerUnavailable(String),
29 /// Connection error.
30 ConnectionError(String),
31 /// Authentication failed.
32 AuthenticationError(String),
33 /// Request rejected by venue.
34 RequestRejected { code: i32, msg: String },
35 /// SBE decoding error.
36 DecodeError(SbeDecodeError),
37 /// Request timed out.
38 Timeout(String),
39 /// Request ID not found in pending requests.
40 UnknownRequestId(String),
41}
42
43impl Display for BinanceWsApiError {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match self {
46 Self::ClientError(msg) => write!(f, "Client error: {msg}"),
47 Self::HandlerUnavailable(msg) => write!(f, "Handler unavailable: {msg}"),
48 Self::ConnectionError(msg) => write!(f, "Connection error: {msg}"),
49 Self::AuthenticationError(msg) => write!(f, "Authentication error: {msg}"),
50 Self::RequestRejected { code, msg } => {
51 write!(f, "Request rejected [{code}]: {msg}")
52 }
53 Self::DecodeError(err) => write!(f, "Decode error: {err}"),
54 Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
55 Self::UnknownRequestId(id) => write!(f, "Unknown request ID: {id}"),
56 }
57 }
58}
59
60impl std::error::Error for BinanceWsApiError {}
61
62impl From<SbeDecodeError> for BinanceWsApiError {
63 fn from(err: SbeDecodeError) -> Self {
64 Self::DecodeError(err)
65 }
66}
67
68/// Result type for Binance WebSocket API operations.
69pub type BinanceWsApiResult<T> = Result<T, BinanceWsApiError>;