atlassian_cli_api/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum ApiError {
5    #[error("HTTP request failed: {0}")]
6    RequestFailed(#[from] reqwest::Error),
7
8    #[error("Rate limit exceeded. Retry after {retry_after} seconds")]
9    RateLimitExceeded { retry_after: u64 },
10
11    #[error("Authentication failed: {message}")]
12    AuthenticationFailed { message: String },
13
14    #[error("Resource not found: {resource}")]
15    NotFound { resource: String },
16
17    #[error("Invalid request: {message}")]
18    BadRequest { message: String },
19
20    #[error("Server error: {status} - {message}")]
21    ServerError { status: u16, message: String },
22
23    #[error("Invalid URL: {0}")]
24    InvalidUrl(#[from] url::ParseError),
25
26    #[error("JSON serialization error: {0}")]
27    JsonError(#[from] serde_json::Error),
28
29    #[error("Request timeout after {attempts} attempts")]
30    Timeout { attempts: usize },
31
32    #[error("Invalid response format: {0}")]
33    InvalidResponse(String),
34}
35
36impl ApiError {
37    pub fn is_retryable(&self) -> bool {
38        match self {
39            ApiError::RateLimitExceeded { .. } => true,
40            ApiError::ServerError { status, .. } if *status >= 500 => true,
41            ApiError::Timeout { .. } => true,
42            _ => false,
43        }
44    }
45
46    pub fn suggestion(&self) -> Option<&str> {
47        match self {
48            ApiError::AuthenticationFailed { .. } => {
49                Some("Verify your API token using: atlassian-cli auth test")
50            }
51            ApiError::RateLimitExceeded { .. } => {
52                Some("Consider reducing request frequency or use bulk operations")
53            }
54            ApiError::NotFound { .. } => Some("Check if the resource ID is correct"),
55            ApiError::BadRequest { .. } => Some("Review the request parameters"),
56            ApiError::Timeout { .. } => Some("Check your network connection or try again later"),
57            _ => None,
58        }
59    }
60}
61
62pub type Result<T> = std::result::Result<T, ApiError>;