csfloat_rs/
error.rs

1use thiserror::Error;
2
3/// Result type alias for CSFloat API operations
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Error types for CSFloat API client
7#[derive(Error, Debug)]
8pub enum Error {
9    #[error("Unauthorized -- Your API key is wrong.")]
10    Unauthorized,
11
12    #[error("Forbidden -- The requested resource is hidden for administrators only.")]
13    Forbidden,
14
15    #[error("Not Found -- The specified resource could not be found.")]
16    NotFound,
17
18    #[error("Method Not Allowed -- You tried to access a resource with an invalid method.")]
19    MethodNotAllowed,
20
21    #[error("Not Acceptable -- You requested a format that isn't json.")]
22    NotAcceptable,
23
24    #[error("Gone -- The requested resource has been removed from our servers.")]
25    Gone,
26
27    #[error("I'm a teapot.")]
28    Teapot,
29
30    #[error("Too Many Requests -- You're requesting too many resources! Slow down!")]
31    TooManyRequests,
32
33    #[error("Internal Server Error -- We had a problem with our server. Try again later.")]
34    InternalServerError,
35
36    #[error("Service Unavailable -- We're temporarily offline for maintenance. Please try again later.")]
37    ServiceUnavailable,
38
39    #[error("HTTP Error: {status} - {body}")]
40    HttpError { status: u16, body: String },
41
42    #[error("Invalid proxy URL format: {0}")]
43    InvalidProxy(String),
44
45    #[error("Request error: {0}")]
46    RequestError(#[from] reqwest::Error),
47
48    #[error("JSON parsing error: {0}")]
49    JsonError(#[from] serde_json::Error),
50
51    #[error("URL parsing error: {0}")]
52    UrlError(#[from] url::ParseError),
53
54    #[error("Invalid parameter: {0}")]
55    InvalidParameter(String),
56
57    #[error("Expected JSON response but got {0}")]
58    UnexpectedContentType(String),
59}
60
61impl Error {
62    /// Create an error from HTTP status code
63    pub fn from_status(status: u16, body: String) -> Self {
64        match status {
65            401 => Error::Unauthorized,
66            403 => Error::Forbidden,
67            404 => Error::NotFound,
68            405 => Error::MethodNotAllowed,
69            406 => Error::NotAcceptable,
70            410 => Error::Gone,
71            418 => Error::Teapot,
72            429 => Error::TooManyRequests,
73            500 => Error::InternalServerError,
74            503 => Error::ServiceUnavailable,
75            _ => Error::HttpError { status, body },
76        }
77    }
78}