sure_client_rs/
error.rs

1use thiserror::Error;
2
3/// The main error type for the Sure API client
4#[derive(Debug, Error)]
5pub enum ApiError {
6    // API-level errors
7    /// Bad request error (400)
8    #[error("Bad request: {message} (status: {status})")]
9    BadRequest {
10        /// The error message from the API
11        message: String,
12        /// The HTTP status code
13        status: reqwest::StatusCode,
14    },
15
16    /// Unauthorized error (401)
17    #[error("Unauthorized: {message}")]
18    Unauthorized {
19        /// The error message from the API
20        message: String,
21    },
22
23    /// Forbidden error (403)
24    #[error("Forbidden: {message}")]
25    Forbidden {
26        /// The error message from the API
27        message: String,
28    },
29
30    /// Not found error (404)
31    #[error("Not found: {message}")]
32    NotFound {
33        /// The error message from the API
34        message: String,
35    },
36
37    /// Unprocessable entity error (422)
38    #[error("Validation error: {message}")]
39    ValidationError {
40        /// The error message from the API
41        message: String,
42    },
43
44    /// Rate limit error (429)
45    #[error("Rate limited: {message}")]
46    RateLimited {
47        /// The error message from the API
48        message: String,
49    },
50
51    /// Internal server error (500)
52    #[error("Internal server error: {message}")]
53    InternalServerError {
54        /// The error message from the API
55        message: String,
56    },
57
58    /// Generic API error
59    #[error("API error {status}: {message}")]
60    ApiError {
61        /// The HTTP status code
62        status: reqwest::StatusCode,
63        /// The error message from the API
64        message: String,
65    },
66
67    // Client-level errors
68    /// Invalid parameter error (client-side validation)
69    #[error("Invalid parameter: {0}")]
70    InvalidParameter(String),
71
72    /// Network error
73    #[error("Network error: {0}")]
74    Network(#[from] reqwest::Error),
75
76    /// Invalid header value
77    #[error("Invalid header value: {0}")]
78    InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
79
80    /// URL parse error
81    #[error("URL parse error: {0}")]
82    UrlParse(#[from] url::ParseError),
83
84    /// JSON deserialization error
85    #[error("JSON deserialization error: {error}. Source: {source_string}")]
86    JsonDeserialization {
87        /// The underlying serde error
88        error: serde_json::Error,
89        /// The source string that failed to deserialize
90        source_string: String,
91    },
92
93    /// JSON serialization error
94    #[error("JSON serialization error: {0}")]
95    JsonSerialization(#[from] serde_json::Error),
96}
97
98/// Result type alias for the Sure API client
99pub type ApiResult<T> = std::result::Result<T, ApiError>;