nautilus_deribit/websocket/
error.rs1use nautilus_network::error::SendError;
19use thiserror::Error;
20use tokio_tungstenite::tungstenite;
21
22#[derive(Debug, Clone, Error)]
24pub enum DeribitWsError {
25 #[error("Not connected")]
27 NotConnected,
28 #[error("Transport error: {0}")]
30 Transport(String),
31 #[error("Send error: {0}")]
33 Send(String),
34 #[error("Order command channel closed")]
36 AdmissionClosed,
37 #[error("Order command canceled before transmission")]
39 CanceledBeforeSend,
40 #[error("Failed to encode order command: {0}")]
42 Encode(String),
43 #[error(transparent)]
46 TransportSend(SendError),
47 #[error("JSON error: {0}")]
49 Json(String),
50 #[error("Authentication error: {0}")]
52 Authentication(String),
53 #[error("Client error: {0}")]
55 ClientError(String),
56 #[error("Parsing error: {0}")]
58 ParsingError(String),
59 #[error("Deribit error {code}: {message}")]
61 DeribitError { code: i64, message: String },
62 #[error("Tungstenite error: {0}")]
64 TungsteniteError(String),
65 #[error("Subscribe error: {0}")]
67 Subscribe(String),
68
69 #[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
92pub type DeribitWsResult<T> = Result<T, DeribitWsError>;
94
95#[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 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}