refactor: replace IntoServiceError trait with direct ServiceError conversions

This commit is contained in:
GW_MC
2025-12-07 14:40:11 +08:00
parent ce404670d6
commit 9c139d6007
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()),
}
}
}