Skip to main content

intr_providers/
error.rs

1//! Provider error types.
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum ProviderError {
7    /// HTTP 401/403 - bad API key or access denied. Do not retry.
8    #[error("authentication failed for provider '{provider}': {message}")]
9    AuthError {
10        provider: &'static str,
11        message: String,
12    },
13
14    /// HTTP 400/422 - we sent bad input. Do not retry.
15    #[error("bad request to provider '{provider}': {message}")]
16    BadRequest {
17        provider: &'static str,
18        message: String,
19    },
20
21    /// HTTP 429 - rate limited. Retryable.
22    #[error("rate limited by provider '{provider}'")]
23    RateLimited { provider: &'static str },
24
25    /// HTTP 5xx or network/timeout error. Retryable.
26    #[error("provider '{provider}' unavailable: {message}")]
27    Unavailable {
28        provider: &'static str,
29        message: String,
30    },
31
32    /// The provider returned a response we couldn't parse.
33    #[error("provider '{provider}' returned unparseable response: {message}")]
34    ParseError {
35        provider: &'static str,
36        message: String,
37    },
38
39    /// No key was configured for IntentryOwned variant.
40    #[error("no API key configured for provider '{provider}'; set {env_var}")]
41    MissingApiKey {
42        provider: &'static str,
43        env_var: &'static str,
44    },
45
46    /// Unknown / catch-all.
47    #[error("provider error: {0}")]
48    Other(#[from] anyhow::Error),
49}
50
51impl ProviderError {
52    /// Returns `true` if the error is transient and the caller should retry.
53    pub fn is_retryable(&self) -> bool {
54        matches!(self, Self::RateLimited { .. } | Self::Unavailable { .. })
55    }
56}