refactor: replace IntoServiceError trait with direct ServiceError conversions
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
pub type ServiceError = Box<dyn std::error::Error + Send + Sync>;
|
||||
use sea_orm::DbErr;
|
||||
|
||||
#[allow(dead_code)] // TODO: remove when used
|
||||
pub trait IntoServiceError {
|
||||
fn into_service_error(self) -> ServiceError;
|
||||
pub enum ServiceError {
|
||||
NotFound(String),
|
||||
DatabaseError(String),
|
||||
Unauthorized(String),
|
||||
InternalError(String),
|
||||
}
|
||||
|
||||
impl<T> IntoServiceError for T
|
||||
where
|
||||
T: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
fn into_service_error(self) -> ServiceError {
|
||||
Box::new(self)
|
||||
impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for ServiceError {
|
||||
fn from(err: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
|
||||
ServiceError::InternalError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DbErr> for ServiceError {
|
||||
fn from(err: DbErr) -> Self {
|
||||
match err {
|
||||
DbErr::RecordNotFound(msg) => ServiceError::NotFound(msg),
|
||||
_ => ServiceError::DatabaseError(err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user