Skip to main content

nautilus_deribit/websocket/
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//! Deribit WebSocket client error types.
17
18use nautilus_network::error::SendError;
19use thiserror::Error;
20use tokio_tungstenite::tungstenite;
21
22/// Error types for the Deribit WebSocket client.
23#[derive(Debug, Clone, Error)]
24pub enum DeribitWsError {
25    /// Client is not connected.
26    #[error("Not connected")]
27    NotConnected,
28    /// Transport-level error during WebSocket communication.
29    #[error("Transport error: {0}")]
30    Transport(String),
31    /// Failed to send message over WebSocket.
32    #[error("Send error: {0}")]
33    Send(String),
34    /// The handler command channel was closed before the order command was queued.
35    #[error("Order command channel closed")]
36    AdmissionClosed,
37    /// The handler was stopping, so the order command was not written.
38    #[error("Order command canceled before transmission")]
39    CanceledBeforeSend,
40    /// Order-command encoding failed before a write started.
41    #[error("Failed to encode order command: {0}")]
42    Encode(String),
43    /// Order-command send failure. `WriteTimeout` and `BrokenPipe` can follow a started write;
44    /// other variants fail before the write.
45    #[error(transparent)]
46    TransportSend(SendError),
47    /// JSON serialization/deserialization error.
48    #[error("JSON error: {0}")]
49    Json(String),
50    /// Authentication failed.
51    #[error("Authentication error: {0}")]
52    Authentication(String),
53    /// Generic client error.
54    #[error("Client error: {0}")]
55    ClientError(String),
56    /// Parsing error during message processing.
57    #[error("Parsing error: {0}")]
58    ParsingError(String),
59    /// Error returned by Deribit API (JSON-RPC error response).
60    #[error("Deribit error {code}: {message}")]
61    DeribitError { code: i64, message: String },
62    /// WebSocket transport error from tungstenite.
63    #[error("Tungstenite error: {0}")]
64    TungsteniteError(String),
65    /// Subscription rejected by server.
66    #[error("Subscribe error: {0}")]
67    Subscribe(String),
68
69    /// Request timeout.
70    #[error("Timeout: {0}")]
71    Timeout(String),
72}
73
74impl From<tungstenite::Error> for DeribitWsError {
75    fn from(error: tungstenite::Error) -> Self {
76        Self::TungsteniteError(error.to_string())
77    }
78}
79
80impl From<serde_json::Error> for DeribitWsError {
81    fn from(error: serde_json::Error) -> Self {
82        Self::Json(error.to_string())
83    }
84}
85
86impl From<String> for DeribitWsError {
87    fn from(msg: String) -> Self {
88        Self::ClientError(msg)
89    }
90}
91
92/// Result type alias for Deribit WebSocket operations.
93pub type DeribitWsResult<T> = Result<T, DeribitWsError>;
94
95/// Determines if an error should trigger a retry.
96#[must_use]
97pub fn should_retry_deribit_ws_error(error: &DeribitWsError) -> bool {
98    match error {
99        DeribitWsError::Transport(_)
100        | DeribitWsError::Send(_)
101        | DeribitWsError::NotConnected
102        | DeribitWsError::Timeout(_) => true,
103        DeribitWsError::DeribitError { code, .. } => {
104            // Deribit retriable error codes
105            matches!(
106                code,
107                10028 | 10040 | 10041 | 10047 | 10066 | 11051 | 11094 | 13028 | 13888
108            )
109        }
110        DeribitWsError::Json(_)
111        | DeribitWsError::Encode(_)
112        | DeribitWsError::Authentication(_)
113        | DeribitWsError::AdmissionClosed
114        | DeribitWsError::CanceledBeforeSend
115        | DeribitWsError::TransportSend(_)
116        | DeribitWsError::ClientError(_)
117        | DeribitWsError::ParsingError(_)
118        | DeribitWsError::TungsteniteError(_)
119        | DeribitWsError::Subscribe(_) => false,
120    }
121}