nautilus_deribit/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//! Deribit WebSocket client error types.
17
18use thiserror::Error;
19use tokio_tungstenite::tungstenite;
20
21/// Error types for the Deribit WebSocket client.
22#[derive(Debug, Clone, Error)]
23pub enum DeribitWsError {
24    /// Client is not connected.
25    #[error("Not connected")]
26    NotConnected,
27    /// Transport-level error during WebSocket communication.
28    #[error("Transport error: {0}")]
29    Transport(String),
30    /// Failed to send message over WebSocket.
31    #[error("Send error: {0}")]
32    Send(String),
33    /// JSON serialization/deserialization error.
34    #[error("JSON error: {0}")]
35    Json(String),
36    /// Authentication failed.
37    #[error("Authentication error: {0}")]
38    Authentication(String),
39    /// Generic client error.
40    #[error("Client error: {0}")]
41    ClientError(String),
42    /// Parsing error during message processing.
43    #[error("Parsing error: {0}")]
44    ParsingError(String),
45    /// Error returned by Deribit API (JSON-RPC error response).
46    #[error("Deribit error {code}: {message}")]
47    DeribitError {
48        /// The error code from Deribit.
49        code: i64,
50        /// The error message from Deribit.
51        message: String,
52    },
53    /// WebSocket transport error from tungstenite.
54    #[error("Tungstenite error: {0}")]
55    TungsteniteError(String),
56    /// Request timeout.
57    #[error("Timeout: {0}")]
58    Timeout(String),
59}
60
61impl From<tungstenite::Error> for DeribitWsError {
62    fn from(error: tungstenite::Error) -> Self {
63        Self::TungsteniteError(error.to_string())
64    }
65}
66
67impl From<serde_json::Error> for DeribitWsError {
68    fn from(error: serde_json::Error) -> Self {
69        Self::Json(error.to_string())
70    }
71}
72
73impl From<String> for DeribitWsError {
74    fn from(msg: String) -> Self {
75        Self::ClientError(msg)
76    }
77}
78
79/// Result type alias for Deribit WebSocket operations.
80pub type DeribitWsResult<T> = Result<T, DeribitWsError>;
81
82/// Determines if an error should trigger a retry.
83#[must_use]
84pub fn should_retry_deribit_ws_error(error: &DeribitWsError) -> bool {
85    match error {
86        DeribitWsError::Transport(_)
87        | DeribitWsError::Send(_)
88        | DeribitWsError::NotConnected
89        | DeribitWsError::Timeout(_) => true,
90        DeribitWsError::DeribitError { code, .. } => {
91            // Deribit retriable error codes
92            matches!(
93                code,
94                10028 | 10040 | 10041 | 10047 | 10066 | 11051 | 11094 | 13028 | 13888
95            )
96        }
97        DeribitWsError::Json(_)
98        | DeribitWsError::Authentication(_)
99        | DeribitWsError::ClientError(_)
100        | DeribitWsError::ParsingError(_)
101        | DeribitWsError::TungsteniteError(_) => false,
102    }
103}