From 9130f18cbbd3203c8cccd2bf603573e298879347 Mon Sep 17 00:00:00 2001 From: "Dotan J. Nahum" Date: Sat, 7 Dec 2024 09:34:07 +0200 Subject: [PATCH] impl: add convenience funcs to ModelError --- src/model/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/model/mod.rs b/src/model/mod.rs index 749b9ea8c..d454e4848 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -35,11 +35,30 @@ pub enum ModelError { #[error(transparent)] Any(#[from] Box), + + #[error("{0}")] + Message(String), } #[allow(clippy::module_name_repetitions)] pub type ModelResult = std::result::Result; +impl ModelError { + #[must_use] + pub fn wrap(err: impl std::error::Error + Send + Sync + 'static) -> Self { + Self::Any(Box::new(err)) + } + + #[must_use] + pub fn to_msg(err: impl std::error::Error + Send + Sync + 'static) -> Self { + Self::Message(err.to_string()) + } + + #[must_use] + pub fn msg(s: &str) -> Self { + Self::Message(s.to_string()) + } +} #[async_trait] pub trait Authenticable: Clone { async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult;