RTSDA-Website/church-core/src/error.rs

62 lines
1.4 KiB
Rust

use thiserror::Error;
pub type Result<T> = std::result::Result<T, ChurchApiError>;
#[derive(Debug, Error)]
pub enum ChurchApiError {
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
#[error("JSON parsing failed: {0}")]
Json(#[from] serde_json::Error),
#[error("Date parsing failed: {0}")]
DateParse(String),
#[error("API returned error: {0}")]
Api(String),
#[error("Authentication failed: {0}")]
Auth(String),
#[error("Cache error: {0}")]
Cache(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Network error: {0}")]
Network(String),
#[error("Timeout error: operation took too long")]
Timeout,
#[error("Rate limit exceeded")]
RateLimit,
#[error("Resource not found")]
NotFound,
#[error("Permission denied")]
PermissionDenied,
#[error("Internal error: {0}")]
Internal(String),
}
impl ChurchApiError {
pub fn is_network_error(&self) -> bool {
matches!(self, Self::Http(_) | Self::Network(_) | Self::Timeout)
}
pub fn is_auth_error(&self) -> bool {
matches!(self, Self::Auth(_) | Self::PermissionDenied)
}
pub fn is_temporary(&self) -> bool {
matches!(self, Self::Timeout | Self::RateLimit | Self::Network(_))
}
}