kalshi_rust/
kalshi_error.rs

1use core::fmt;
2use std::error::Error;
3// CUSTOM ERROR STRUCTS + ENUMS
4// -----------------------------------------------
5
6/// A comprehensive set of errors that might occur in the Kalshi module.
7///
8/// This enum encompasses various types of errors, including HTTP request errors,
9/// user input errors, and internal errors. It provides a unified error type for
10/// the entire Kalshi module.
11///
12#[derive(Debug)]
13pub enum KalshiError {
14    /// Errors that occur during HTTP requests. This includes connectivity issues,
15    /// response serialization problems, and HTTP status errors.
16    RequestError(RequestError),
17    /// Errors caused by incorrect or invalid user input.
18    UserInputError(String),
19    /// Errors representing unexpected internal issues or situations that are not supposed to happen.
20    InternalError(String),
21    /// Authentication errors, such as missing credentials or invalid keys.
22    Auth(String),
23    // TODO: add error type specifically for joining threads together.
24}
25
26impl fmt::Display for KalshiError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            KalshiError::RequestError(e) => write!(f, "HTTP Error: {}", e),
30            KalshiError::UserInputError(e) => write!(f, "User Input Error: {}", e),
31            KalshiError::InternalError(e) => write!(f, "INTERNAL ERROR, PLEASE EMAIL DEVELOPER OR MAKE A NEW ISSUE ON THE CRATE'S REPOSITORY: https://github.com/dpeachpeach/kalshi-rust. Specific Error: {}", e),
32            KalshiError::Auth(e) => write!(f, "Authentication Error: {}", e)
33        }
34    }
35}
36
37impl Error for KalshiError {
38    fn source(&self) -> Option<&(dyn Error + 'static)> {
39        match self {
40            KalshiError::RequestError(e) => Some(e),
41            KalshiError::UserInputError(_) => None,
42            KalshiError::InternalError(_) => None,
43            KalshiError::Auth(_) => None,
44        }
45    }
46}
47
48impl From<reqwest::Error> for KalshiError {
49    fn from(err: reqwest::Error) -> Self {
50        if err.is_decode() {
51            KalshiError::RequestError(RequestError::SerializationError(err))
52        } else if err.is_status() {
53            if let Some(status) = err.status() {
54                if status.is_client_error() {
55                    KalshiError::RequestError(RequestError::ClientError(err))
56                } else if status.is_server_error() {
57                    KalshiError::RequestError(RequestError::ServerError(err))
58                } else {
59                    KalshiError::InternalError(
60                        "Theoretically Impossible Error. Internal code 1".to_string(),
61                    )
62                }
63            } else {
64                KalshiError::RequestError(RequestError::ServerError(err))
65            }
66        } else if err.is_body() || err.is_timeout() {
67            KalshiError::RequestError(RequestError::ServerError(err))
68        } else {
69            KalshiError::InternalError(
70                "Theoretically Impossible Error. Internal code 2".to_string(),
71            )
72        }
73    }
74}
75
76/// Specific kinds of HTTP request errors encountered in the Kalshi module.
77///
78/// This enum categorizes errors related to HTTP requests, including serialization errors, client-side errors,
79/// and server-side errors.
80///
81#[derive(Debug)]
82pub enum RequestError {
83    /// Errors occurring during serialization or deserialization of request or response data.
84    SerializationError(reqwest::Error),
85    /// Errors representing client-side request issues, such as bad requests or unauthorized access.
86    ClientError(reqwest::Error),
87    /// Errors indicating server-side issues, like internal server errors or service unavailability.
88    ServerError(reqwest::Error),
89}
90
91impl fmt::Display for RequestError {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        match self {
94            RequestError::SerializationError(e) => write!(f, "Serialization Error. You connected successfully but either: Your inputs to a request were incorrect or the exchange is closed! {}", e),
95            RequestError::ClientError(e) => {
96                if let Some(status) = e.status() {
97                    write!(f, "Client Request Error, Status code: {}", status)
98                } else {
99                    write!(f, "Client Request Error: {}", e)
100                }
101            },
102            RequestError::ServerError(e) => {
103                if let Some(status) = e.status() {
104                    write!(f, "Server Request Error: Status code: {}", status)
105                } else {
106                    write!(f, "Server Request Error: {}", e)
107                }
108            },
109        }
110    }
111}
112
113impl Error for RequestError {
114    fn source(&self) -> Option<&(dyn Error + 'static)> {
115        match self {
116            RequestError::ClientError(e) => Some(e),
117            RequestError::ServerError(e) => Some(e),
118            RequestError::SerializationError(e) => Some(e),
119        }
120    }
121}
122
123impl From<std::io::Error> for KalshiError {
124    fn from(err: std::io::Error) -> Self {
125        KalshiError::Auth(format!("IO Error: {}", err))
126    }
127}
128
129impl From<openssl::error::ErrorStack> for KalshiError {
130    fn from(err: openssl::error::ErrorStack) -> Self {
131        KalshiError::Auth(format!("OpenSSL Error: {}", err))
132    }
133}
134
135impl From<reqwest::header::InvalidHeaderValue> for KalshiError {
136    fn from(err: reqwest::header::InvalidHeaderValue) -> Self {
137        KalshiError::Auth(format!("Invalid Header Value: {}", err))
138    }
139}
140
141impl From<http::method::InvalidMethod> for KalshiError {
142    fn from(err: http::method::InvalidMethod) -> Self {
143        KalshiError::Auth(format!("Invalid HTTP Method: {}", err))
144    }
145}
146
147impl From<url::ParseError> for KalshiError {
148    fn from(err: url::ParseError) -> Self {
149        KalshiError::UserInputError(format!("URL Parse Error: {}", err))
150    }
151}
152
153impl From<serde_urlencoded::ser::Error> for KalshiError {
154    fn from(err: serde_urlencoded::ser::Error) -> Self {
155        KalshiError::UserInputError(format!("URL Encoding Error: {}", err))
156    }
157}
158
159impl From<serde_json::Error> for KalshiError {
160    fn from(err: serde_json::Error) -> Self {
161        KalshiError::InternalError(format!("JSON Error: {}", err))
162    }
163}