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

View File

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