polymarket_hft/
error.rs

1//! Error types for the Polymarket SDK.
2//!
3//! This module defines all error types that can occur when using the SDK.
4
5/// The main error type for the Polymarket SDK.
6#[derive(Debug, thiserror::Error)]
7pub enum PolymarketError {
8    /// HTTP request failed.
9    #[error("HTTP request failed: {0}")]
10    Http(#[from] reqwest::Error),
11
12    /// HTTP middleware request failed.
13    #[error("HTTP request failed: {0}")]
14    HttpMiddleware(#[from] reqwest_middleware::Error),
15
16    /// WebSocket connection or communication error.
17    #[error("WebSocket error: {0}")]
18    WebSocket(String),
19
20    /// API returned an error response.
21    #[error("API error: {0}")]
22    Api(String),
23
24    /// URL parsing error.
25    #[error("URL parsing error: {0}")]
26    Url(#[from] url::ParseError),
27
28    /// Bad request - invalid parameters or input.
29    #[error("Bad request: {0}")]
30    BadRequest(String),
31
32    /// Serialization or deserialization error.
33    #[error("Serialization error: {0}")]
34    Serde(#[from] serde_json::Error),
35
36    /// Generic error with custom message.
37    #[error("{0}")]
38    Other(String),
39}
40
41/// A specialized Result type for Polymarket SDK operations.
42pub type Result<T> = std::result::Result<T, PolymarketError>;
43
44impl PolymarketError {
45    /// Creates a new WebSocket error.
46    pub fn websocket<S: Into<String>>(msg: S) -> Self {
47        Self::WebSocket(msg.into())
48    }
49
50    /// Creates a new API error.
51    pub fn api<S: Into<String>>(msg: S) -> Self {
52        Self::Api(msg.into())
53    }
54
55    /// Creates a new bad request error.
56    pub fn bad_request<S: Into<String>>(msg: S) -> Self {
57        Self::BadRequest(msg.into())
58    }
59
60    /// Creates a generic error.
61    pub fn other<S: Into<String>>(msg: S) -> Self {
62        Self::Other(msg.into())
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_websocket_error_creation() {
72        let err = PolymarketError::websocket("connection failed");
73        assert!(matches!(err, PolymarketError::WebSocket(_)));
74        assert!(err.to_string().contains("connection failed"));
75    }
76
77    #[test]
78    fn test_api_error_creation() {
79        let err = PolymarketError::api("rate limited");
80        assert!(matches!(err, PolymarketError::Api(_)));
81        assert!(err.to_string().contains("rate limited"));
82    }
83
84    #[test]
85    fn test_bad_request_error_creation() {
86        let err = PolymarketError::bad_request("invalid parameter");
87        assert!(matches!(err, PolymarketError::BadRequest(_)));
88        assert!(err.to_string().contains("invalid parameter"));
89    }
90
91    #[test]
92    fn test_other_error_creation() {
93        let err = PolymarketError::other("something went wrong");
94        assert!(matches!(err, PolymarketError::Other(_)));
95        assert_eq!(err.to_string(), "something went wrong");
96    }
97
98    #[test]
99    fn test_display_websocket() {
100        let err = PolymarketError::WebSocket("test".to_string());
101        assert_eq!(err.to_string(), "WebSocket error: test");
102    }
103
104    #[test]
105    fn test_display_api() {
106        let err = PolymarketError::Api("test".to_string());
107        assert_eq!(err.to_string(), "API error: test");
108    }
109
110    #[test]
111    fn test_display_bad_request() {
112        let err = PolymarketError::BadRequest("test".to_string());
113        assert_eq!(err.to_string(), "Bad request: test");
114    }
115
116    #[test]
117    fn test_from_url_parse_error() {
118        let url_err = url::Url::parse("not a url").unwrap_err();
119        let err: PolymarketError = url_err.into();
120        assert!(matches!(err, PolymarketError::Url(_)));
121        assert!(err.to_string().contains("URL parsing error"));
122    }
123
124    #[test]
125    fn test_from_serde_error() {
126        let json_err = serde_json::from_str::<String>("not valid json").unwrap_err();
127        let err: PolymarketError = json_err.into();
128        assert!(matches!(err, PolymarketError::Serde(_)));
129        assert!(err.to_string().contains("Serialization error"));
130    }
131}