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(#[from] tokio_tungstenite::tungstenite::Error),
25    #[error(transparent)]
26    Decode(#[from] serde_json::Error),
27}
28
29impl PolymarketUsError {
30    pub fn from_status(status: reqwest::StatusCode, message: String) -> Self {
31        match status.as_u16() {
32            400 => Self::BadRequest(message),
33            401 => Self::Authentication(message),
34            404 => Self::NotFound(message),
35            429 => Self::RateLimited(message),
36            500 | 502 | 503 | 504 => Self::Server(message),
37            code => Self::Api {
38                status: code,
39                message,
40            },
41        }
42    }
43}