Skip to main content

dd_api/
error.rs

1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, ApiError>;
4
5#[derive(Debug, Error)]
6pub enum ApiError {
7    #[error("authentication failed (401/403): check DD_API_KEY and DD_APP_KEY")]
8    Auth,
9
10    #[error("not found (404): {0}")]
11    NotFound(String),
12
13    #[error("rate limited (429); retry after {retry_after_secs}s")]
14    RateLimited { retry_after_secs: u64 },
15
16    #[error("upstream error {status}: {body}")]
17    Upstream { status: u16, body: String },
18
19    #[error("network error: {0}")]
20    Network(#[from] reqwest::Error),
21
22    #[error("url error: {0}")]
23    Url(#[from] url::ParseError),
24
25    #[error("decode error: {0}")]
26    Decode(#[from] serde_json::Error),
27}
28
29impl ApiError {
30    /// Exit code mapping matching the SPECIFICATION.
31    pub fn exit_code(&self) -> i32 {
32        match self {
33            ApiError::Auth => 2,
34            ApiError::NotFound(_) => 3,
35            ApiError::RateLimited { .. } => 4,
36            ApiError::Upstream { .. } => 5,
37            ApiError::Network(_) => 6,
38            _ => 1,
39        }
40    }
41}