nautilus_bybit/websocket/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//! Error types for Bybit WebSocket client operations.
17
18use nautilus_network::error::SendError;
19use thiserror::Error;
20
21/// Result alias for Bybit WebSocket operations.
22pub type BybitWsResult<T> = Result<T, BybitWsError>;
23
24/// Error type for Bybit WebSocket client failures.
25#[derive(Clone, Debug, Error)]
26pub enum BybitWsError {
27 /// The WebSocket client is not currently connected.
28 #[error("WebSocket not connected")]
29 NotConnected,
30
31 /// Failed to send a message over the WebSocket connection.
32 #[error("WebSocket send error: {0}")]
33 Send(String),
34
35 /// Underlying transport error from the WebSocket implementation.
36 #[error("WebSocket transport error: {0}")]
37 Transport(String),
38
39 /// Failed to parse or serialize JSON payloads.
40 #[error("JSON error: {0}")]
41 Json(String),
42
43 /// Authentication handshake failed or timed out.
44 #[error("Authentication error: {0}")]
45 Authentication(String),
46
47 /// Client-side validation or logic error.
48 #[error("Client error: {0}")]
49 ClientError(String),
50}
51
52impl From<SendError> for BybitWsError {
53 fn from(error: SendError) -> Self {
54 Self::Send(error.to_string())
55 }
56}
57
58impl From<tokio_tungstenite::tungstenite::Error> for BybitWsError {
59 fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
60 Self::Transport(error.to_string())
61 }
62}
63
64impl From<serde_json::Error> for BybitWsError {
65 fn from(error: serde_json::Error) -> Self {
66 Self::Json(error.to_string())
67 }
68}
69
70impl From<String> for BybitWsError {
71 fn from(msg: String) -> Self {
72 Self::Authentication(msg)
73 }
74}
75
76/// Determines if a Bybit WebSocket error should trigger a retry.
77pub(crate) fn should_retry_bybit_error(error: &BybitWsError) -> bool {
78 match error {
79 BybitWsError::Transport(_) => true,
80 BybitWsError::Send(_) => true,
81 BybitWsError::ClientError(msg) => {
82 let msg_lower = msg.to_lowercase();
83 msg_lower.contains("timeout")
84 || msg_lower.contains("timed out")
85 || msg_lower.contains("connection")
86 || msg_lower.contains("network")
87 }
88 BybitWsError::NotConnected => true,
89 BybitWsError::Authentication(_) | BybitWsError::Json(_) => false,
90 }
91}
92
93/// Creates a timeout error for Bybit operations.
94pub(crate) fn create_bybit_timeout_error(msg: String) -> BybitWsError {
95 BybitWsError::ClientError(msg)
96}