use std::fmt; #[derive(Debug)] pub enum UserError { BadRequest(String), Unauthorized, Forbidden, NotFound, Conflict(String), Internal(String), } impl fmt::Display for UserError { 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::Internal(message) => write!(f, "{message}"), } } } impl std::error::Error for UserError {} impl From for UserError { fn from(error: space::errors::SpaceError) -> Self { match error { space::errors::SpaceError::BadRequest(message) => Self::BadRequest(message), space::errors::SpaceError::Unauthorized => Self::Unauthorized, space::errors::SpaceError::Forbidden => Self::Forbidden, space::errors::SpaceError::NotFound => Self::NotFound, space::errors::SpaceError::Conflict(message) => Self::Conflict(message), other => Self::Internal(other.to_string()), } } } export::db_error_conversions!(UserError); export::storage_error_conversion!(UserError); export::redis_error_conversion!(UserError); export::export_error_conversion!(UserError); export::json_payload_conversion!(UserError); export::api_error! { log: "user", 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"), Internal(500, "internal server error"), } } impl From for ApiError { fn from(error: UserError) -> Self { match error { UserError::BadRequest(msg) => Self::bad_request(msg), UserError::Unauthorized => Self::unauthorized(), UserError::Forbidden => Self::forbidden(), UserError::NotFound => Self::not_found("user resource not found"), UserError::Conflict(msg) => Self::conflict(msg), UserError::Internal(message) => Self::internal(message), } } }