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