1use thiserror::Error;
2
3#[derive(Error, Debug)]
5pub enum ApiError {
6 #[error("API error: {status} - {message}")]
8 Api { status: u16, message: String },
9
10 #[error("Authentication failed: {0}")]
12 Authentication(String),
13
14 #[error("Validation error: {0}")]
16 Validation(String),
17
18 #[error("Rate limit exceeded: {0}")]
20 RateLimit(String),
21
22 #[error("Request timeout")]
24 Timeout,
25
26 #[error("Network error: {0}")]
28 Network(#[from] reqwest::Error),
29
30 #[error("Serialization error: {0}")]
32 Serialization(#[from] serde_json::Error),
33
34 #[error("URL error: {0}")]
36 Url(#[from] url::ParseError),
37}
38
39impl ApiError {
40 pub async fn from_response(response: reqwest::Response) -> Self {
42 let status = response.status().as_u16();
43
44 let body_text = response.text().await.unwrap_or_default();
46 tracing::debug!("API error response body: {}", body_text);
47
48 let message = serde_json::from_str::<serde_json::Value>(&body_text)
49 .ok()
50 .and_then(|v| {
51 v.get("error")
52 .or(v.get("message"))
53 .and_then(|m| m.as_str())
54 .map(String::from)
55 })
56 .unwrap_or_else(|| body_text.clone());
57
58 match status {
59 401 | 403 => Self::Authentication(message),
60 400 => Self::Validation(message),
61 429 => Self::RateLimit(message),
62 408 => Self::Timeout,
63 _ => Self::Api { status, message },
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_rate_limit_error_carries_message() {
74 let err = ApiError::RateLimit("too many requests, retry after 5s".to_string());
75 let display = format!("{}", err);
76 assert!(
77 display.contains("too many requests"),
78 "RateLimit display should contain the message: {}",
79 display
80 );
81 }
82
83 #[test]
84 fn test_rate_limit_error_display_format() {
85 let err = ApiError::RateLimit("slow down".to_string());
86 assert_eq!(format!("{}", err), "Rate limit exceeded: slow down");
87 }
88}