feat: convert db errors to service errors

This commit is contained in:
realaravinth 2022-05-10 23:48:50 +05:30
parent f337721b25
commit 43aac949e3
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88

View file

@ -24,6 +24,7 @@ use actix_web::{
HttpResponse, HttpResponseBuilder,
};
use argon2_creds::errors::CredsError;
use db_core::errors::DBError;
use derive_more::{Display, Error};
use lettre::transport::smtp::Error as SmtpError;
use libmcaptcha::errors::CaptchaError;
@ -35,6 +36,15 @@ use validator::ValidationErrors;
#[derive(Debug, Display, Error)]
pub struct SmtpErrorWrapper(SmtpError);
#[derive(Debug, Display, Error)]
pub struct DBErrorWrapper(DBError);
impl std::cmp::PartialEq for DBErrorWrapper {
fn eq(&self, other: &Self) -> bool {
format!("{}", self.0) == format!("{}", other.0)
}
}
impl std::cmp::PartialEq for SmtpErrorWrapper {
fn eq(&self, other: &Self) -> bool {
self.0.status() == other.0.status()
@ -103,6 +113,9 @@ pub enum ServiceError {
#[display(fmt = "{}", _0)]
CaptchaError(CaptchaError),
#[display(fmt = "{}", _0)]
DBError(DBErrorWrapper),
}
#[derive(Serialize, Deserialize)]
@ -160,6 +173,8 @@ impl ResponseError for ServiceError {
log::error!("{}", e.0);
StatusCode::INTERNAL_SERVER_ERROR
}
ServiceError::DBError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
@ -179,6 +194,13 @@ impl From<CredsError> for ServiceError {
}
}
impl From<DBError> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(e: DBError) -> ServiceError {
ServiceError::DBError(DBErrorWrapper(e))
}
}
impl From<ValidationErrors> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(_: ValidationErrors) -> ServiceError {