redis_enterprise/
error.rs

1//! Error types for REST API operations
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum RestError {
7    #[error("Invalid URL: {0}")]
8    InvalidUrl(String),
9
10    #[error("HTTP request failed: {0}")]
11    RequestFailed(#[from] reqwest::Error),
12
13    #[error("Authentication failed")]
14    AuthenticationFailed,
15
16    #[error("API error: {message} (code: {code})")]
17    ApiError { code: u16, message: String },
18
19    #[error("Serialization error: {0}")]
20    SerializationError(#[from] serde_json::Error),
21
22    #[error("Parse error: {0}")]
23    ParseError(String),
24
25    #[error("Connection error: {0}")]
26    ConnectionError(String),
27
28    #[error("Not connected to REST API")]
29    NotConnected,
30
31    #[error("Validation error: {0}")]
32    ValidationError(String),
33
34    #[error("Resource not found")]
35    NotFound,
36
37    #[error("Unauthorized")]
38    Unauthorized,
39
40    #[error("Server error: {0}")]
41    ServerError(String),
42}
43
44impl RestError {
45    /// Check if this is a not found error
46    pub fn is_not_found(&self) -> bool {
47        matches!(self, RestError::NotFound)
48            || matches!(self, RestError::ApiError { code, .. } if *code == 404)
49    }
50
51    /// Check if this is an authentication error
52    pub fn is_unauthorized(&self) -> bool {
53        matches!(self, RestError::Unauthorized)
54            || matches!(self, RestError::AuthenticationFailed)
55            || matches!(self, RestError::ApiError { code, .. } if *code == 401)
56    }
57
58    /// Check if this is a server error
59    pub fn is_server_error(&self) -> bool {
60        matches!(self, RestError::ServerError(_))
61            || matches!(self, RestError::ApiError { code, .. } if *code >= 500)
62    }
63}
64
65pub type Result<T> = std::result::Result<T, RestError>;