use sea_orm::DbErr; #[derive(Debug)] pub enum ServiceError { NotFound(String), DatabaseError(String), Unauthorized(String), InternalError(String), BadRequest(String), } impl From> for ServiceError { fn from(err: Box) -> Self { ServiceError::InternalError(err.to_string()) } } impl std::fmt::Display for ServiceError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ServiceError::NotFound(msg) => write!(f, "Not Found: {}", msg), ServiceError::DatabaseError(msg) => write!(f, "Database Error: {}", msg), ServiceError::Unauthorized(msg) => write!(f, "Unauthorized: {}", msg), ServiceError::InternalError(msg) => write!(f, "Internal Error: {}", msg), ServiceError::BadRequest(msg) => write!(f, "Bad Request: {}", msg), } } } impl std::error::Error for ServiceError {} impl From for ServiceError { fn from(err: DbErr) -> Self { match err { DbErr::RecordNotFound(msg) => ServiceError::NotFound(msg), _ => ServiceError::DatabaseError(err.to_string()), } } }