polymarket_client_sdk/rtds/
error.rs

1#![expect(
2    clippy::module_name_repetitions,
3    reason = "Error types include the module name to indicate their scope"
4)]
5
6use std::error::Error as StdError;
7use std::fmt;
8
9/// RTDS WebSocket error variants.
10#[non_exhaustive]
11#[derive(Debug)]
12pub enum RtdsError {
13    /// Error connecting to or communicating with the WebSocket server
14    Connection(tokio_tungstenite::tungstenite::Error),
15    /// Error parsing a WebSocket message
16    MessageParse(serde_json::Error),
17    /// Subscription request failed
18    SubscriptionFailed(String),
19    /// Authentication failed for protected topic
20    AuthenticationFailed,
21    /// WebSocket connection was closed
22    ConnectionClosed,
23    /// Operation timed out
24    Timeout,
25    /// Received an invalid or unexpected message
26    InvalidMessage(String),
27    /// Subscription stream lagged and missed messages
28    Lagged {
29        /// Number of messages that were missed
30        count: u64,
31    },
32}
33
34impl fmt::Display for RtdsError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::Connection(err) => write!(f, "RTDS WebSocket connection error: {err}"),
38            Self::MessageParse(err) => write!(f, "Failed to parse RTDS message: {err}"),
39            Self::SubscriptionFailed(reason) => write!(f, "RTDS subscription failed: {reason}"),
40            Self::AuthenticationFailed => write!(f, "RTDS WebSocket authentication failed"),
41            Self::ConnectionClosed => write!(f, "RTDS WebSocket connection closed"),
42            Self::Timeout => write!(f, "RTDS WebSocket operation timed out"),
43            Self::InvalidMessage(msg) => write!(f, "Invalid RTDS message: {msg}"),
44            Self::Lagged { count } => {
45                write!(f, "RTDS subscription lagged, missed {count} messages")
46            }
47        }
48    }
49}
50
51impl StdError for RtdsError {
52    fn source(&self) -> Option<&(dyn StdError + 'static)> {
53        match self {
54            Self::Connection(err) => Some(err),
55            Self::MessageParse(err) => Some(err),
56            _ => None,
57        }
58    }
59}
60
61// Integration with main Error type
62impl From<RtdsError> for crate::error::Error {
63    fn from(err: RtdsError) -> Self {
64        crate::error::Error::with_source(crate::error::Kind::WebSocket, err)
65    }
66}