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 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 /// Subscription rejected by server.
57 #[error("Subscribe error: {0}")]
58 Subscribe(String),
59
60 /// Request timeout.
61 #[error("Timeout: {0}")]
62 Timeout(String),
63}
64
65impl From<tungstenite::Error> for DeribitWsError {
66 fn from(error: tungstenite::Error) -> Self {
67 Self::TungsteniteError(error.to_string())
68 }
69}
70
71impl From<serde_json::Error> for DeribitWsError {
72 fn from(error: serde_json::Error) -> Self {
73 Self::Json(error.to_string())
74 }
75}
76
77impl From<String> for DeribitWsError {
78 fn from(msg: String) -> Self {
79 Self::ClientError(msg)
80 }
81}
82
83/// Result type alias for Deribit WebSocket operations.
84pub type DeribitWsResult<T> = Result<T, DeribitWsError>;
85
86/// Determines if an error should trigger a retry.
87#[must_use]
88pub fn should_retry_deribit_ws_error(error: &DeribitWsError) -> bool {
89 match error {
90 DeribitWsError::Transport(_)
91 | DeribitWsError::Send(_)
92 | DeribitWsError::NotConnected
93 | DeribitWsError::Timeout(_) => true,
94 DeribitWsError::DeribitError { code, .. } => {
95 // Deribit retriable error codes
96 matches!(
97 code,
98 10028 | 10040 | 10041 | 10047 | 10066 | 11051 | 11094 | 13028 | 13888
99 )
100 }
101 DeribitWsError::Json(_)
102 | DeribitWsError::Authentication(_)
103 | DeribitWsError::ClientError(_)
104 | DeribitWsError::ParsingError(_)
105 | DeribitWsError::TungsteniteError(_)
106 | DeribitWsError::Subscribe(_) => false,
107 }
108}