Skip to main content

kalshi_pmx/
error.rs

1//! Error and result types used by the SDK.
2
3use http::header::InvalidHeaderValue;
4use thiserror::Error;
5
6/// SDK result type.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors returned by REST, WebSocket, authentication, parsing, and helper APIs.
10#[derive(Debug, Error)]
11pub enum Error {
12    /// RSA signing or credential parsing failed.
13    #[error("authentication error: {0}")]
14    Auth(String),
15
16    /// An authenticated endpoint was used without configured credentials.
17    #[error("missing API credentials")]
18    MissingCredentials,
19
20    /// The HTTP client failed before receiving an API response.
21    #[error("HTTP request failed: {0}")]
22    Http(#[from] reqwest::Error),
23
24    /// Kalshi returned a non-2xx HTTP response.
25    #[error("Kalshi API returned {status}: {body}")]
26    Api {
27        /// HTTP status code returned by Kalshi.
28        status: reqwest::StatusCode,
29        /// Response body returned by Kalshi, preserved as text.
30        body: String,
31    },
32
33    /// JSON serialization or deserialization failed.
34    #[error("JSON error: {0}")]
35    Json(#[from] serde_json::Error),
36
37    /// URL parsing failed.
38    #[error("URL parse error: {0}")]
39    Url(#[from] url::ParseError),
40
41    /// An authentication header could not be represented as an HTTP header value.
42    #[error("invalid header value: {0}")]
43    InvalidHeaderValue(#[from] InvalidHeaderValue),
44
45    /// HTTP request construction failed.
46    #[error("HTTP request build error: {0}")]
47    HttpBuild(#[from] http::Error),
48
49    /// WebSocket transport failed.
50    #[error("WebSocket error: {0}")]
51    WebSocket(#[from] tokio_tungstenite::tungstenite::Error),
52
53    /// File or network I/O failed outside reqwest/tungstenite.
54    #[error("I/O error: {0}")]
55    Io(#[from] std::io::Error),
56
57    /// An operation exceeded its configured timeout.
58    #[error("timeout")]
59    Timeout,
60
61    /// A WebSocket stream closed and could not be reconnected.
62    #[error("stream closed")]
63    StreamClosed,
64
65    /// Client-side rate-limit settings were invalid.
66    #[error("invalid rate limit config: {0}")]
67    InvalidRateLimitConfig(String),
68
69    /// Environment-based configuration was invalid.
70    #[error("environment configuration error: {0}")]
71    Env(String),
72
73    /// An orderbook delta could not be safely applied to local state.
74    #[error("orderbook out of sync: {0}")]
75    OrderbookOutOfSync(String),
76
77    /// A fixed-point decimal string was not in the supported format.
78    #[error("invalid fixed point value `{0}`")]
79    InvalidFixedPoint(String),
80
81    /// A catch-all for errors produced by extension code.
82    #[error("{0}")]
83    Other(String),
84}