use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("Request failed: {0}")]
Request(#[from] reqwest::Error),
#[error("API error ({status}): {message}")]
Api { status: u16, message: String },
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Request timeout after {0}ms")]
Timeout(u64),
#[error("Invalid API key")]
InvalidApiKey,
#[error("Resource not found: {0}")]
NotFound(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Domain not verified: {0}")]
DomainNotVerified(String),
#[error("XML parsing error: {0}")]
XmlParse(String),
}
impl Error {
pub fn is_not_found(&self) -> bool {
matches!(self, Error::NotFound(_) | Error::Api { status: 404, .. })
}
pub fn is_auth_error(&self) -> bool {
matches!(self, Error::InvalidApiKey | Error::Api { status: 401, .. })
}
pub fn is_validation_error(&self) -> bool {
matches!(self, Error::Validation(_) | Error::Api { status: 400, .. })
}
pub fn status_code(&self) -> Option<u16> {
match self {
Error::Api { status, .. } => Some(*status),
Error::NotFound(_) => Some(404),
Error::InvalidApiKey => Some(401),
Error::Validation(_) => Some(400),
Error::DomainNotVerified(_) => Some(403),
_ => None,
}
}
}