use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
#[error("API error ({status}): {message}")]
Api {
status: u16,
message: String,
},
#[error("Bad request: {0}")]
BadRequest(String),
#[error("Authentication failed: {0}")]
AuthenticationError(String),
#[error("Permission denied: {0}")]
PermissionDenied(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Conflict: {0}")]
Conflict(String),
#[error("Unprocessable entity: {0}")]
UnprocessableEntity(String),
#[error("Rate limit exceeded: {0}")]
RateLimitExceeded(String),
#[error("Internal server error: {0}")]
InternalServerError(String),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Invalid API key: API key must be provided either via constructor or ZEROENTROPY_API_KEY environment variable")]
InvalidApiKey,
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Base64 error: {0}")]
Base64(#[from] base64::DecodeError),
}
impl Error {
pub fn from_status(status: u16, message: String) -> Self {
match status {
400 => Error::BadRequest(message),
401 => Error::AuthenticationError(message),
403 => Error::PermissionDenied(message),
404 => Error::NotFound(message),
409 => Error::Conflict(message),
422 => Error::UnprocessableEntity(message),
429 => Error::RateLimitExceeded(message),
500..=599 => Error::InternalServerError(message),
_ => Error::Api { status, message },
}
}
}