Skip to main content

axonflow_sdk_rust/
error.rs

1use crate::types::decisions::RateLimitEnvelope;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum AxonFlowError {
6    #[error("HTTP request failed: {0}")]
7    HttpError(#[from] reqwest::Error),
8    #[error("Serialization/Deserialization failed: {0}")]
9    SerdeError(#[from] serde_json::Error),
10    #[error("API error ({status}): {message}")]
11    ApiError { status: u16, message: String },
12    /// Tier-cap 429 with a parsed V1 upgrade envelope. Distinct from a
13    /// generic 429 ApiError because callers should branch on the upgrade
14    /// fields (tier / compare_url / buy_url) without re-parsing the
15    /// raw body. Mirrors the cross-SDK 429-with-envelope pattern
16    /// (#1982 / #1958). Boxed to keep `AxonFlowError` small —
17    /// `RateLimitEnvelope` is ~176 bytes and would dominate the enum
18    /// otherwise (clippy::result_large_err).
19    #[error("Rate limited (tier={}, limit_type={}): {}", .envelope.tier, .envelope.limit_type, .envelope.error)]
20    RateLimited { envelope: Box<RateLimitEnvelope> },
21    #[error("Configuration error: {0}")]
22    ConfigError(String),
23    #[error("AxonFlow platform is unavailable: {0}")]
24    Unavailable(String),
25}
26
27impl AxonFlowError {
28    pub fn is_retryable(&self) -> bool {
29        match self {
30            AxonFlowError::HttpError(e) => e.is_timeout() || e.is_connect(),
31            AxonFlowError::ApiError { status, .. } => *status >= 500 || *status == 429,
32            AxonFlowError::RateLimited { .. } => true,
33            AxonFlowError::Unavailable(_) => true,
34            _ => false,
35        }
36    }
37
38    pub fn is_fail_open_eligible(&self) -> bool {
39        match self {
40            AxonFlowError::HttpError(e) => e.is_timeout() || e.is_connect(),
41            AxonFlowError::ApiError { status, .. } => *status >= 500 || *status == 429,
42            AxonFlowError::RateLimited { .. } => true,
43            AxonFlowError::Unavailable(_) => true,
44            _ => false,
45        }
46    }
47}