mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2024-11-26 19:36:01 +03:00
feat: convert db errors to service errors
This commit is contained in:
parent
f337721b25
commit
43aac949e3
1 changed files with 22 additions and 0 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue