use std::fmt; #[derive(Debug)] pub enum AccessError { InvalidArgument(String), Unauthorized, Forbidden, NotFound, Internal(String), } impl fmt::Display for AccessError { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::InvalidArgument(message) | Self::Internal(message) => { formatter.write_str(message) } Self::Unauthorized => formatter.write_str("authentication required"), Self::Forbidden => formatter.write_str("operation not allowed"), Self::NotFound => formatter.write_str("resource not found"), } } } impl std::error::Error for AccessError {} impl From for AccessError { fn from(error: sea_orm::DbErr) -> Self { Self::Internal(error.to_string()) } } #[macro_export] macro_rules! access_error_conversion { ($target:ident, $invalid:ident) => { impl From<$crate::AccessError> for $target { fn from(error: $crate::AccessError) -> Self { match error { $crate::AccessError::InvalidArgument(message) => Self::$invalid(message), $crate::AccessError::Unauthorized => Self::Unauthorized, $crate::AccessError::Forbidden => Self::Forbidden, $crate::AccessError::NotFound => Self::NotFound, $crate::AccessError::Internal(message) => Self::Internal(message), } } } }; }