rusty_common/websocket/
error.rs

1//! WebSocket error types
2//!
3//! Provides comprehensive error handling for WebSocket operations.
4
5use thiserror::Error;
6use yawc::WebSocketError as YawcError;
7
8/// Result type for WebSocket operations
9pub type WebSocketResult<T> = Result<T, WebSocketError>;
10
11/// WebSocket errors
12#[derive(Error, Debug)]
13pub enum WebSocketError {
14    /// Connection errors
15    #[error("Failed to connect to WebSocket: {0}")]
16    ConnectionError(String),
17
18    /// Authentication errors
19    #[error("WebSocket authentication failed: {0}")]
20    AuthenticationError(String),
21
22    /// Subscription errors
23    #[error("WebSocket subscription failed: {0}")]
24    SubscriptionError(String),
25
26    /// Message processing errors
27    #[error("Failed to process WebSocket message: {0}")]
28    MessageProcessingError(String),
29
30    /// Protocol errors
31    #[error("Protocol error: {0}")]
32    ProtocolError(String),
33
34    /// Connection closed
35    #[error("WebSocket connection closed: {0}")]
36    ConnectionClosed(String),
37
38    /// Timeout errors
39    #[error("WebSocket operation timed out after {0} milliseconds")]
40    Timeout(u64),
41
42    /// Rate limit errors
43    #[error("WebSocket rate limit exceeded: {0}")]
44    RateLimitExceeded(String),
45
46    /// Reconnection errors
47    #[error("Reconnection failed after {attempts} attempts")]
48    ReconnectionFailed {
49        /// The number of attempts made.
50        attempts: u32,
51    },
52
53    /// Not connected
54    #[error("WebSocket is not connected")]
55    NotConnected,
56
57    /// Channel errors
58    #[error("Channel error: {0}")]
59    ChannelError(String),
60
61    /// I/O errors
62    #[error("I/O error: {0}")]
63    IoError(#[from] std::io::Error),
64
65    /// URL parsing errors
66    #[error("Invalid URL: {0}")]
67    InvalidUrl(#[from] url::ParseError),
68
69    /// Yawc errors
70    #[error("WebSocket error: {0}")]
71    YawcError(#[from] YawcError),
72
73    /// JSON errors
74    #[error("JSON error: {0}")]
75    JsonError(#[from] simd_json::Error),
76
77    /// Other errors
78    #[error("{0}")]
79    Other(String),
80}
81
82impl From<String> for WebSocketError {
83    fn from(s: String) -> Self {
84        WebSocketError::Other(s)
85    }
86}
87
88impl From<&str> for WebSocketError {
89    fn from(s: &str) -> Self {
90        WebSocketError::Other(s.to_string())
91    }
92}