Skip to main content

uptrakit_openapi_client/
error.rs

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