Skip to content

Commit

Permalink
impl: add convenience funcs to ModelError
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Dec 7, 2024
1 parent 1737be8 commit 9130f18
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,30 @@ pub enum ModelError {

#[error(transparent)]
Any(#[from] Box<dyn std::error::Error + Send + Sync>),

#[error("{0}")]
Message(String),
}

#[allow(clippy::module_name_repetitions)]
pub type ModelResult<T, E = ModelError> = std::result::Result<T, E>;

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<Self>;
Expand Down

0 comments on commit 9130f18

Please sign in to comment.