atlassian_cli_api/
error.rs1use 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("Access forbidden: {message}")]
15 Forbidden { message: String },
16
17 #[error("Resource not found: {resource}")]
18 NotFound { resource: String },
19
20 #[error("Invalid request: {message}")]
21 BadRequest { message: String },
22
23 #[error("Server error: {status} - {message}")]
24 ServerError { status: u16, message: String },
25
26 #[error("Invalid URL: {0}")]
27 InvalidUrl(#[from] url::ParseError),
28
29 #[error("JSON serialization error: {0}")]
30 JsonError(#[from] serde_json::Error),
31
32 #[error("Request timeout after {attempts} attempts")]
33 Timeout { attempts: usize },
34
35 #[error("API endpoint removed: {message}")]
36 EndpointGone { message: String },
37
38 #[error("Invalid response format: {0}")]
39 InvalidResponse(String),
40}
41
42impl ApiError {
43 pub fn is_retryable(&self) -> bool {
44 match self {
45 ApiError::RateLimitExceeded { .. } => true,
46 ApiError::ServerError { status, .. } if *status >= 500 => true,
47 ApiError::Timeout { .. } => true,
48 ApiError::EndpointGone { .. } => false,
49 _ => false,
50 }
51 }
52
53 pub fn suggestion(&self) -> Option<String> {
54 match self {
55 ApiError::AuthenticationFailed { .. } => {
56 Some("Verify tokens with: atlassian-cli auth list\nTest auth with: atlassian-cli auth test [--bitbucket]".to_string())
57 }
58 ApiError::Forbidden { message } => {
59 let base = "Verify tokens with: atlassian-cli auth list\nTest auth with: atlassian-cli auth test [--bitbucket]".to_string();
60 let lower = message.to_lowercase();
61 if lower.contains("scope") || lower.contains("privilege") || lower.contains("permission") {
62 Some(format!("{base}\nAdd missing scopes at: https://bitbucket.org/account/settings/app-passwords/"))
63 } else {
64 Some(base)
65 }
66 }
67 ApiError::RateLimitExceeded { .. } => {
68 Some("Consider reducing request frequency or use bulk operations".to_string())
69 }
70 ApiError::NotFound { .. } => Some("Check if the resource ID is correct".to_string()),
71 ApiError::BadRequest { message } => {
72 if message.contains("Version number must be 1") {
73 Some("This is a draft page. Use 'confluence page publish' to publish for the first time".to_string())
74 } else if message.to_lowercase().contains("version") {
75 Some("Version conflict detected. The content may have been modified. Fetch latest and retry".to_string())
76 } else {
77 Some("Review the request parameters".to_string())
78 }
79 }
80 ApiError::Timeout { .. } => Some("Check your network connection or try again later".to_string()),
81 ApiError::EndpointGone { .. } => {
82 Some("This API endpoint has been removed by Atlassian. Update atlassian-cli to the latest version.".to_string())
83 }
84 _ => None,
85 }
86 }
87}
88
89pub type Result<T> = std::result::Result<T, ApiError>;
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn forbidden_has_suggestion() {
97 let err = ApiError::Forbidden {
98 message: "no access".to_string(),
99 };
100 assert!(err.suggestion().is_some());
101 assert!(err.suggestion().unwrap().contains("auth test"));
102 }
103
104 #[test]
105 fn forbidden_is_not_retryable() {
106 let err = ApiError::Forbidden {
107 message: "no access".to_string(),
108 };
109 assert!(!err.is_retryable());
110 }
111
112 #[test]
113 fn authentication_failed_has_suggestion() {
114 let err = ApiError::AuthenticationFailed {
115 message: "expired".to_string(),
116 };
117 assert!(err.suggestion().is_some());
118 assert!(err.suggestion().unwrap().contains("auth test"));
119 }
120
121 #[test]
122 fn forbidden_with_scope_message_includes_app_passwords_link() {
123 let err = ApiError::Forbidden {
124 message: "Your credentials lack the required scope.".to_string(),
125 };
126 let hint = err.suggestion().unwrap();
127 assert!(hint.contains("auth test"));
128 assert!(hint.contains("app-passwords"));
129 }
130
131 #[test]
132 fn forbidden_without_scope_omits_app_passwords_link() {
133 let err = ApiError::Forbidden {
134 message: "no access".to_string(),
135 };
136 let hint = err.suggestion().unwrap();
137 assert!(hint.contains("auth test"));
138 assert!(!hint.contains("app-passwords"));
139 }
140
141 #[test]
142 fn forbidden_with_permission_message_includes_link() {
143 let err = ApiError::Forbidden {
144 message: "Insufficient Permission to access this resource".to_string(),
145 };
146 let hint = err.suggestion().unwrap();
147 assert!(hint.contains("app-passwords"));
148 }
149
150 #[test]
151 fn endpoint_gone_has_suggestion() {
152 let err = ApiError::EndpointGone {
153 message: "The requested API has been removed".to_string(),
154 };
155 assert!(err.suggestion().is_some());
156 assert!(err.suggestion().unwrap().contains("Update atlassian-cli"));
157 }
158
159 #[test]
160 fn endpoint_gone_is_not_retryable() {
161 let err = ApiError::EndpointGone {
162 message: "removed".to_string(),
163 };
164 assert!(!err.is_retryable());
165 }
166}