Skip to main content

uptrakit_openapi_client/
error.rs

1use rootcause::prelude::*;
2use uptrakit_shared_macros::impl_report_conversion;
3
4/// Errors that can occur when communicating with the Uptrakit API.
5#[derive(Debug, thiserror::Error)]
6pub enum ClientError {
7    #[error("HTTP request failed: {0}")]
8    Http(#[from] reqwest::Error),
9
10    #[error("JSON error: {0}")]
11    Json(#[from] serde_json::Error),
12
13    #[error("API error ({status}): {message}")]
14    Api {
15        status: reqwest::StatusCode,
16        message: String,
17    },
18
19    #[error("rate limited{}", match .retry_after_seconds {
20        Some(secs) => format!(" (retry after {secs}s)"),
21        None => String::new(),
22    })]
23    RateLimited { retry_after_seconds: Option<u64> },
24
25    #[error("not found: {0}")]
26    NotFound(String),
27
28    #[error("not authenticated")]
29    NotAuthenticated,
30
31    #[error("invalid HTTP method: {0}")]
32    InvalidMethod(String),
33}
34
35pub type Result<T> = std::result::Result<T, Report<ClientError>>;
36
37impl_report_conversion! {
38    reqwest::Error  => ClientError::Http,
39    serde_json::Error => ClientError::Json,
40}