Skip to main content

configvault_sdk/
errors.rs

1use thiserror::Error;
2
3/// Errors that can occur when using the ConfigVault SDK.
4#[derive(Debug, Error)]
5pub enum ConfigVaultError {
6    /// The requested configuration key was not found.
7    #[error("configuration key '{key}' not found")]
8    NotFound { key: String },
9
10    /// Authentication failed (invalid or missing API key).
11    #[error("authentication failed")]
12    Authentication,
13
14    /// The ConfigVault service is unavailable.
15    #[error("service unavailable")]
16    ServiceUnavailable,
17
18    /// An HTTP request error occurred.
19    #[error("request failed: {0}")]
20    Request(#[from] reqwest::Error),
21
22    /// An unexpected error occurred.
23    #[error("unexpected error: {message}")]
24    Unexpected { status: u16, message: String },
25}
26
27/// Maps an HTTP error response to the appropriate `ConfigVaultError` variant.
28pub(crate) fn handle_error_response(
29    status: u16,
30    key: Option<&str>,
31) -> ConfigVaultError {
32    match status {
33        401 => ConfigVaultError::Authentication,
34        404 => ConfigVaultError::NotFound {
35            key: key.unwrap_or("unknown").to_string(),
36        },
37        503 => ConfigVaultError::ServiceUnavailable,
38        code => ConfigVaultError::Unexpected {
39            status: code,
40            message: format!("HTTP {code}"),
41        },
42    }
43}