Skip to main content

nautilus_okx/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//! Error types produced by the OKX WebSocket client implementation.
17
18use thiserror::Error;
19use tokio_tungstenite::tungstenite;
20
21/// A typed error enumeration for the OKX WebSocket client.
22#[derive(Debug, Clone, Error)]
23pub enum OKXWsError {
24    #[error("Parsing error: {0}")]
25    ParsingError(String),
26    /// Errors returned directly by OKX (non-zero code).
27    #[error("OKX error {error_code}: {message}")]
28    OkxError { error_code: String, message: String },
29    /// Failure during JSON serialization/deserialization.
30    #[error("JSON error: {0}")]
31    JsonError(String),
32    #[error("Client error: {0}")]
33    ClientError(String),
34    #[error("No active WebSocket client")]
35    NoActiveClient,
36    #[error("Handler not available: {0}")]
37    HandlerUnavailable(String),
38    #[error("Send failed: {0}")]
39    SendFailed(String),
40    #[error("Operation timed out after {timeout_ms}ms")]
41    OperationTimeout { timeout_ms: u64 },
42    #[error("Authentication error: {0}")]
43    AuthenticationError(String),
44    /// Wrapping the underlying HttpClientError from the network crate.
45    // #[error("Network error: {0}")]
46    // WebSocketClientError(WebSocketClientError),  // TODO: Implement Debug
47    /// WebSocket transport error.
48    #[error("Tungstenite error: {0}")]
49    TungsteniteError(String),
50}
51
52impl From<tungstenite::Error> for OKXWsError {
53    fn from(error: tungstenite::Error) -> Self {
54        Self::TungsteniteError(error.to_string())
55    }
56}
57
58impl From<String> for OKXWsError {
59    fn from(msg: String) -> Self {
60        Self::AuthenticationError(msg)
61    }
62}