Compare commits
2 Commits
documentat
...
9c139d6007
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c139d6007 | ||
| ce404670d6 |
@@ -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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use sea_orm::{
|
||||
IntoActiveModel, QueryFilter,
|
||||
};
|
||||
|
||||
use crate::errors::service_error::{IntoServiceError, ServiceError};
|
||||
use crate::errors::service_error::ServiceError;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait SettingsStore: Send + Sync {
|
||||
@@ -37,11 +37,11 @@ impl SettingsStore for SettingsService {
|
||||
.await;
|
||||
|
||||
match setting {
|
||||
Err(err) => Err(err.into_service_error()),
|
||||
Ok(None) => Err(
|
||||
DbErr::RecordNotFound(format!("Setting with key '{}' not found", key))
|
||||
.into_service_error(),
|
||||
),
|
||||
Err(err) => Err(ServiceError::from(err)),
|
||||
Ok(None) => Err(ServiceError::from(DbErr::RecordNotFound(format!(
|
||||
"Setting with key '{}' not found",
|
||||
key
|
||||
)))),
|
||||
Ok(Some(record)) => Ok(record.value),
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ impl SettingsStore for SettingsService {
|
||||
new_record
|
||||
.insert(&*self.connection)
|
||||
.await
|
||||
.map_err(|err| err.into_service_error())
|
||||
.map_err(ServiceError::from)
|
||||
};
|
||||
|
||||
match existing {
|
||||
@@ -71,7 +71,7 @@ impl SettingsStore for SettingsService {
|
||||
handle_not_found(key.to_string(), value).await?;
|
||||
}
|
||||
_ => {
|
||||
return Err(Box::new(err));
|
||||
return Err(ServiceError::from(err));
|
||||
}
|
||||
},
|
||||
Ok(None) => {
|
||||
@@ -83,7 +83,7 @@ impl SettingsStore for SettingsService {
|
||||
.into_active_model()
|
||||
.update(&*self.connection)
|
||||
.await
|
||||
.map_err(|err| err.into_service_error())?;
|
||||
.map_err(ServiceError::from)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user