use sea_orm::DbErr;
use toolkit_db::DbError;
use toolkit_db::secure::ScopeError;
use crate::domain::error::ChatEngineError;
impl From<DbErr> for ChatEngineError {
fn from(err: DbErr) -> Self {
match err {
DbErr::RecordNotFound(msg) => Self::NotFound {
resource: "record",
id: msg,
},
other => {
Self::Internal {
reason: "database error".to_owned(),
source: Some(Box::new(other)),
}
}
}
}
}
impl From<DbError> for ChatEngineError {
fn from(err: DbError) -> Self {
match err {
DbError::Sea(sea) => sea.into(),
other => Self::Internal {
reason: "database error".to_owned(),
source: Some(Box::new(other)),
},
}
}
}
impl From<ScopeError> for ChatEngineError {
fn from(err: ScopeError) -> Self {
match err {
ScopeError::Db(sea) => sea.into(),
ScopeError::Denied(msg) => Self::Forbidden {
reason: msg.to_owned(),
},
ScopeError::TenantNotInScope { tenant_id } => Self::Forbidden {
reason: format!("tenant {tenant_id} not in scope"),
},
ScopeError::Invalid(msg) => Self::Internal {
reason: format!("invalid scope: {msg}"),
source: None,
},
}
}
}
#[cfg(test)]
#[path = "error_map_tests.rs"]
mod error_map_tests;