use std::fmt; pub use export::is_unique_violation; #[derive(Debug)] pub enum ChatError { BadRequest(String), Unauthorized, Forbidden, NotFound, Conflict(String), RateLimited(String), Internal(String), } impl fmt::Display for ChatError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::BadRequest(message) => write!(f, "{message}"), Self::Unauthorized => write!(f, "unauthorized"), Self::Forbidden => write!(f, "forbidden"), Self::NotFound => write!(f, "not found"), Self::Conflict(message) => write!(f, "{message}"), Self::RateLimited(message) => write!(f, "{message}"), Self::Internal(message) => write!(f, "{message}"), } } } impl std::error::Error for ChatError {} export::db_error_conversions!(ChatError); export::storage_error_conversion!(ChatError); export::redis_error_conversion!(ChatError); export::export_error_conversion!(ChatError); export::json_payload_conversion!(ChatError); export::access_error_conversion!(ChatError, BadRequest); impl From for ChatError { fn from(error: dir::Error) -> Self { match error { dir::Error::InvalidInput(message) => Self::BadRequest(message), dir::Error::NotFound => Self::NotFound, dir::Error::Database(error) => Self::from(error), } } } export::api_error! { log: "chat", pub enum ApiError { BadRequest(400, "request failed validation"), Unauthorized(401, "authentication required"), Forbidden(403, "operation not allowed"), NotFound(404, "resource not found"), Conflict(409, "resource conflict"), TooManyRequests(429, "rate limit exceeded"), Internal(500, "internal server error"), } } impl From for ApiError { fn from(error: ChatError) -> Self { match error { ChatError::BadRequest(msg) => Self::bad_request(msg), ChatError::Unauthorized => Self::unauthorized(), ChatError::Forbidden => Self::forbidden(), ChatError::NotFound => Self::not_found("chat resource not found"), ChatError::Conflict(msg) => Self::conflict(msg), ChatError::RateLimited(msg) => Self::rate_limited(msg), ChatError::Internal(message) => Self::internal(message), } } }