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 credentials: {0}")]
11 InvalidCredentials(String),
12 #[error("invalid stream configuration: {0}")]
13 InvalidStreamConfig(String),
14 #[error("stream idle for {0:?} with no frame from the server")]
17 StreamIdle(std::time::Duration),
18 #[error("bad request: {0}")]
19 BadRequest(String),
20 #[error("authentication failed: {0}")]
21 Authentication(String),
22 #[error("resource not found: {0}")]
23 NotFound(String),
24 #[error("rate limit exceeded: {message}")]
25 RateLimited {
26 message: String,
27 retry_after: Option<std::time::Duration>,
29 },
30 #[error("internal server error: {0}")]
31 Server(String),
32 #[error("api error {status}: {message}")]
33 Api { status: u16, message: String },
34 #[error(transparent)]
35 Transport(#[from] reqwest::Error),
36 #[error(transparent)]
37 WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
38 #[error(transparent)]
39 Decode(#[from] serde_json::Error),
40}
41
42impl From<tokio_tungstenite::tungstenite::Error> for PolymarketUsError {
43 fn from(value: tokio_tungstenite::tungstenite::Error) -> Self {
44 Self::WebSocket(Box::new(value))
45 }
46}
47
48impl PolymarketUsError {
49 pub fn from_status(status: reqwest::StatusCode, message: String) -> Self {
50 match status.as_u16() {
51 400 => Self::BadRequest(message),
52 401 => Self::Authentication(message),
53 404 => Self::NotFound(message),
54 429 => Self::RateLimited {
55 message,
56 retry_after: None,
57 },
58 500 | 502 | 503 | 504 => Self::Server(message),
59 code => Self::Api {
60 status: code,
61 message,
62 },
63 }
64 }
65}