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 enum ServiceError {
|
||||||
pub trait IntoServiceError {
|
NotFound(String),
|
||||||
fn into_service_error(self) -> ServiceError;
|
DatabaseError(String),
|
||||||
|
Unauthorized(String),
|
||||||
|
InternalError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> IntoServiceError for T
|
impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for ServiceError {
|
||||||
where
|
fn from(err: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
|
||||||
T: std::error::Error + Send + Sync + 'static,
|
ServiceError::InternalError(err.to_string())
|
||||||
{
|
}
|
||||||
fn into_service_error(self) -> ServiceError {
|
}
|
||||||
Box::new(self)
|
|
||||||
|
impl From<DbErr> for ServiceError {
|
||||||
|
fn from(err: DbErr) -> Self {
|
||||||
|
match err {
|
||||||
|
DbErr::RecordNotFound(msg) => ServiceError::NotFound(msg),
|
||||||
|
_ => ServiceError::DatabaseError(err.to_string()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use sea_orm::{
|
|||||||
IntoActiveModel, QueryFilter,
|
IntoActiveModel, QueryFilter,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::errors::service_error::{IntoServiceError, ServiceError};
|
use crate::errors::service_error::ServiceError;
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait SettingsStore: Send + Sync {
|
pub trait SettingsStore: Send + Sync {
|
||||||
@@ -37,11 +37,11 @@ impl SettingsStore for SettingsService {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
match setting {
|
match setting {
|
||||||
Err(err) => Err(err.into_service_error()),
|
Err(err) => Err(ServiceError::from(err)),
|
||||||
Ok(None) => Err(
|
Ok(None) => Err(ServiceError::from(DbErr::RecordNotFound(format!(
|
||||||
DbErr::RecordNotFound(format!("Setting with key '{}' not found", key))
|
"Setting with key '{}' not found",
|
||||||
.into_service_error(),
|
key
|
||||||
),
|
)))),
|
||||||
Ok(Some(record)) => Ok(record.value),
|
Ok(Some(record)) => Ok(record.value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ impl SettingsStore for SettingsService {
|
|||||||
new_record
|
new_record
|
||||||
.insert(&*self.connection)
|
.insert(&*self.connection)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| err.into_service_error())
|
.map_err(ServiceError::from)
|
||||||
};
|
};
|
||||||
|
|
||||||
match existing {
|
match existing {
|
||||||
@@ -71,7 +71,7 @@ impl SettingsStore for SettingsService {
|
|||||||
handle_not_found(key.to_string(), value).await?;
|
handle_not_found(key.to_string(), value).await?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Box::new(err));
|
return Err(ServiceError::from(err));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
@@ -83,7 +83,7 @@ impl SettingsStore for SettingsService {
|
|||||||
.into_active_model()
|
.into_active_model()
|
||||||
.update(&*self.connection)
|
.update(&*self.connection)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| err.into_service_error())?;
|
.map_err(ServiceError::from)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user