Skip to main content

polymarket_us/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum PolymarketUsError {
5    #[error("authentication required for endpoint {0}")]
6    MissingAuth(&'static str),
7    #[error("invalid stream configuration: {0}")]
8    InvalidStreamConfig(String),
9    #[error("bad request: {0}")]
10    BadRequest(String),
11    #[error("authentication failed: {0}")]
12    Authentication(String),
13    #[error("resource not found: {0}")]
14    NotFound(String),
15    #[error("rate limit exceeded: {0}")]
16    RateLimited(String),
17    #[error("internal server error: {0}")]
18    Server(String),
19    #[error("api error {status}: {message}")]
20    Api { status: u16, message: String },
21    #[error(transparent)]
22    Transport(#[from] reqwest::Error),
23    #[error(transparent)]
24    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
25    #[error(transparent)]
26    Decode(#[from] serde_json::Error),
27}
28
29impl From<tokio_tungstenite::tungstenite::Error> for PolymarketUsError {
30    fn from(value: tokio_tungstenite::tungstenite::Error) -> Self {
31        Self::WebSocket(Box::new(value))
32    }
33}
34
35impl PolymarketUsError {
36    pub fn from_status(status: reqwest::StatusCode, message: String) -> Self {
37        match status.as_u16() {
38            400 => Self::BadRequest(message),
39            401 => Self::Authentication(message),
40            404 => Self::NotFound(message),
41            429 => Self::RateLimited(message),
42            500 | 502 | 503 | 504 => Self::Server(message),
43            code => Self::Api {
44                status: code,
45                message,
46            },
47        }
48    }
49}