2 Commits

Author SHA1 Message Date
GW_MC
9c139d6007 refactor: replace IntoServiceError trait with direct ServiceError conversions 2025-12-07 14:40:11 +08:00
ce404670d6 Merge pull request 'Basic Documentation' (#8) from documentation into master
All checks were successful
Verify / verify-generated-code (push) Successful in 53s
Verify / verify-openapi-spec (push) Successful in 6s
Verify / verify-frontend-api-client (push) Successful in 8s
Test / test-frontend (push) Successful in 21s
Test / frontend-build (push) Successful in 24s
Test / lint (push) Successful in 1m9s
Test / test (push) Successful in 1m14s
Reviewed-on: #8
2025-12-05 22:50:20 +08:00
2 changed files with 27 additions and 19 deletions

View File

@@ -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()),
}
} }
} }

View File

@@ -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)?;
} }
} }