akahu_client/error.rs
1/// Common Akahu error types as per the documentation.
2///
3/// [<https://developers.akahu.nz/docs/response-formatting#common-error-messages>]
4#[derive(Debug, thiserror::Error)]
5pub enum AkahuError {
6 // API-level errors (from Akahu responses)
7 /// Bad request - invalid request parameters
8 #[error("Bad request: {message}")]
9 BadRequest {
10 /// Error message from the API
11 message: String,
12 /// HTTP status code (400)
13 status: u16,
14 },
15
16 /// Unauthorized - invalid or revoked authentication credentials
17 #[error("Unauthorized: {message}")]
18 Unauthorized {
19 /// Error message from the API
20 message: String,
21 },
22
23 /// Forbidden - insufficient permissions or missing required headers
24 #[error("Forbidden: {message}")]
25 Forbidden {
26 /// Error message from the API
27 message: String,
28 },
29
30 /// Not found - resource doesn't exist or is inaccessible
31 #[error("Not found: {message}")]
32 NotFound {
33 /// Error message from the API
34 message: String,
35 },
36
37 /// Rate limited - too many requests
38 #[error("Rate limited: {message}")]
39 RateLimited {
40 /// Error message from the API
41 message: String,
42 },
43
44 /// Internal server error - system-level failure
45 #[error("Internal server error: {message}")]
46 InternalServerError {
47 /// Error message from the API
48 message: String,
49 },
50
51 /// Generic API error with status code and message
52 #[error("API error {status}: {message}")]
53 ApiError {
54 /// HTTP status code
55 status: u16,
56 /// Error message from the API
57 message: String,
58 },
59
60 // Client-level errors
61 /// Network error from reqwest
62 #[error("Network error: {0}")]
63 Network(#[from] reqwest::Error),
64
65 /// Invalid header value
66 #[error("Invalid header value: {0}")]
67 InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
68
69 /// URL parse error
70 #[error("URL parse error: {0}")]
71 UrlParse(#[from] url::ParseError),
72
73 /// JSON deserialization error
74 #[error("JSON deserialization error: {error}{}", .source_string.as_ref().map(|s| format!(" - {}", s)).unwrap_or_default())]
75 JsonDeserialization {
76 /// The deserialisation error that was generated.
77 error: serde_json::Error,
78 /// The string that was originally being deserialized, if available.
79 source_string: Option<String>,
80 },
81
82 /// Missing app secret - call with_app_secret() first for app-scoped endpoints
83 #[error("Missing app secret - call with_app_secret() first")]
84 MissingAppSecret,
85
86 // OAuth-specific errors
87 /// OAuth error response (follows OAuth2 spec)
88 #[error("OAuth error: {error}{}", .error_description.as_ref().map(|d| format!(" - {}", d)).unwrap_or_default())]
89 OAuth {
90 /// OAuth error code (e.g., "invalid_grant")
91 error: String,
92 /// Optional human-readable error description
93 error_description: Option<String>,
94 },
95}
96
97/// Convenience type alias for Results using AkahuError
98pub type AkahuResult<T> = std::result::Result<T, AkahuError>;