zeroentropy_community/
error.rs

1use thiserror::Error;
2
3/// Result type for ZeroEntropy operations
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Error types for the ZeroEntropy SDK
7#[derive(Error, Debug)]
8pub enum Error {
9    /// HTTP request failed
10    #[error("HTTP request failed: {0}")]
11    Http(#[from] reqwest::Error),
12
13    /// API returned an error status code
14    #[error("API error ({status}): {message}")]
15    Api {
16        status: u16,
17        message: String,
18    },
19
20    /// Bad request (400)
21    #[error("Bad request: {0}")]
22    BadRequest(String),
23
24    /// Authentication error (401)
25    #[error("Authentication failed: {0}")]
26    AuthenticationError(String),
27
28    /// Permission denied (403)
29    #[error("Permission denied: {0}")]
30    PermissionDenied(String),
31
32    /// Resource not found (404)
33    #[error("Not found: {0}")]
34    NotFound(String),
35
36    /// Conflict (409) - resource already exists
37    #[error("Conflict: {0}")]
38    Conflict(String),
39
40    /// Unprocessable entity (422)
41    #[error("Unprocessable entity: {0}")]
42    UnprocessableEntity(String),
43
44    /// Rate limit exceeded (429)
45    #[error("Rate limit exceeded: {0}")]
46    RateLimitExceeded(String),
47
48    /// Internal server error (500+)
49    #[error("Internal server error: {0}")]
50    InternalServerError(String),
51
52    /// Failed to serialize/deserialize JSON
53    #[error("JSON error: {0}")]
54    Json(#[from] serde_json::Error),
55
56    /// Invalid API key
57    #[error("Invalid API key: API key must be provided either via constructor or ZEROENTROPY_API_KEY environment variable")]
58    InvalidApiKey,
59
60    /// IO error
61    #[error("IO error: {0}")]
62    Io(#[from] std::io::Error),
63
64    /// Base64 decoding error
65    #[error("Base64 error: {0}")]
66    Base64(#[from] base64::DecodeError),
67}
68
69impl Error {
70    /// Create an API error from response status and message
71    pub fn from_status(status: u16, message: String) -> Self {
72        match status {
73            400 => Error::BadRequest(message),
74            401 => Error::AuthenticationError(message),
75            403 => Error::PermissionDenied(message),
76            404 => Error::NotFound(message),
77            409 => Error::Conflict(message),
78            422 => Error::UnprocessableEntity(message),
79            429 => Error::RateLimitExceeded(message),
80            500..=599 => Error::InternalServerError(message),
81            _ => Error::Api { status, message },
82        }
83    }
84}