#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ModelError {
#[error("Entity already exists")]
EntityAlreadyExists,
#[error("Entity not found")]
EntityNotFound,
#[error(transparent)]
Validation(#[from] crate::validation::ModelValidationErrors),
#[cfg(has_seaorm)]
#[error(transparent)]
DbErr(#[from] sea_orm::DbErr),
#[cfg(has_sqlx)]
#[error(transparent)]
Sqlx(#[from] sqlx::Error),
#[cfg(has_diesel)]
#[error(transparent)]
Diesel(#[from] crate::error::DieselError),
#[cfg(has_toasty)]
#[error(transparent)]
Toasty(#[from] crate::error::toasty_crate::Error),
#[error(transparent)]
Any(#[from] Box<dyn std::error::Error + Send + Sync>),
#[error("{0}")]
Message(String),
}
#[cfg(has_diesel)]
impl From<crate::error::diesel_crates::diesel::result::Error> for ModelError {
fn from(error: crate::error::diesel_crates::diesel::result::Error) -> Self {
Self::Diesel(crate::error::DieselError::Query(error))
}
}
macro_rules! model_from_diesel_crate {
($($feature:literal => $krate:ident),*) => {$(
#[cfg(feature = $feature)]
impl From<$krate::Error> for ModelError {
fn from(error: $krate::Error) -> Self {
Self::Diesel(error.into())
}
}
)*};
}
model_from_diesel_crate!(
"postgres-diesel" => axa_postgres_diesel,
"mysql-diesel" => axa_mysql_diesel,
"sqlite-diesel" => axa_sqlite_diesel
);
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(message: &str) -> Self {
Self::Message(message.to_string())
}
}