nautilus_bitmex/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 definitions for the BitMEX WebSocket client.
17
18use thiserror::Error;
19use tokio_tungstenite::tungstenite;
20
21/// A typed error enumeration for the BitMEX WebSocket client.
22#[derive(Debug, Error)]
23pub enum BitmexWsError {
24    /// Parsing error.
25    #[error("Parsing error: {0}")]
26    ParsingError(String),
27    /// Errors returned directly by BitMEX (non-zero code).
28    #[error("BitMEX error {error_name}: {message}")]
29    BitmexError { error_name: String, message: String },
30    /// Failure during JSON serialization/deserialization.
31    #[error("JSON error: {0}")]
32    JsonError(String),
33    /// Client error.
34    #[error("Client error: {0}")]
35    ClientError(String),
36    /// Authentication error.
37    #[error("Authentication error: {0}")]
38    AuthenticationError(String),
39    /// Subscription error.
40    #[error("Subscription error: {0}")]
41    SubscriptionError(String),
42    /// WebSocket transport error.
43    #[error("Tungstenite error: {0}")]
44    TungsteniteError(#[from] tungstenite::Error),
45    /// Missing credentials for authenticated operation.
46    #[error("Missing credentials: API authentication required for this operation")]
47    MissingCredentials,
48}
49
50impl From<serde_json::Error> for BitmexWsError {
51    fn from(error: serde_json::Error) -> Self {
52        Self::JsonError(error.to_string())
53    }
54}
55
56impl From<String> for BitmexWsError {
57    fn from(msg: String) -> Self {
58        Self::AuthenticationError(msg)
59    }
60}
61
62////////////////////////////////////////////////////////////////////////////////
63// Tests
64////////////////////////////////////////////////////////////////////////////////
65
66#[cfg(test)]
67mod tests {
68    use rstest::rstest;
69
70    use super::*;
71
72    #[rstest]
73    fn test_bitmex_ws_error_display() {
74        let error = BitmexWsError::ParsingError("Invalid message format".to_string());
75        assert_eq!(error.to_string(), "Parsing error: Invalid message format");
76
77        let error = BitmexWsError::BitmexError {
78            error_name: "InvalidTopic".to_string(),
79            message: "Unknown subscription topic".to_string(),
80        };
81        assert_eq!(
82            error.to_string(),
83            "BitMEX error InvalidTopic: Unknown subscription topic"
84        );
85
86        let error = BitmexWsError::ClientError("Connection lost".to_string());
87        assert_eq!(error.to_string(), "Client error: Connection lost");
88
89        let error = BitmexWsError::AuthenticationError("Invalid API key".to_string());
90        assert_eq!(error.to_string(), "Authentication error: Invalid API key");
91
92        let error = BitmexWsError::SubscriptionError("Topic not available".to_string());
93        assert_eq!(error.to_string(), "Subscription error: Topic not available");
94
95        let error = BitmexWsError::MissingCredentials;
96        assert_eq!(
97            error.to_string(),
98            "Missing credentials: API authentication required for this operation"
99        );
100    }
101
102    #[rstest]
103    fn test_bitmex_ws_error_from_json_error() {
104        let json_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
105        let ws_error: BitmexWsError = json_err.into();
106        assert!(ws_error.to_string().contains("JSON error"));
107    }
108
109    #[rstest]
110    fn test_bitmex_ws_error_from_tungstenite() {
111        use tokio_tungstenite::tungstenite::Error as WsError;
112
113        let tungstenite_err = WsError::ConnectionClosed;
114        let ws_error: BitmexWsError = tungstenite_err.into();
115        assert!(ws_error.to_string().contains("Tungstenite error"));
116    }
117}