From ddb6d336f71d00f551bacf465eac11ca90bbb6a9 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 13 May 2022 19:08:31 +0530 Subject: [PATCH] feat: implement accountnotfound and captcha notfound err vals for sqlx postgres --- db/db-sqlx-postgres/src/errors.rs | 9 +++++++++ src/errors.rs | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/db/db-sqlx-postgres/src/errors.rs b/db/db-sqlx-postgres/src/errors.rs index cd2350d7..00e3b333 100644 --- a/db/db-sqlx-postgres/src/errors.rs +++ b/db/db-sqlx-postgres/src/errors.rs @@ -21,6 +21,15 @@ use std::borrow::Cow; use db_core::dev::*; use sqlx::Error; +/// map custom row not found error to DB error +pub fn map_row_not_found_err(e: Error, row_not_found: DBError) -> DBError { + if let Error::RowNotFound = e { + row_not_found + } else { + map_register_err(e) + } +} + /// map postgres errors to [DBError](DBError) types pub fn map_register_err(e: Error) -> DBError { if let Error::Database(err) = e { diff --git a/src/errors.rs b/src/errors.rs index 108358df..c0d15d8e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -106,7 +106,6 @@ pub enum ServiceError { #[display(fmt = "Unable to send email, contact admin")] UnableToSendEmail(SmtpErrorWrapper), - /// when the a token name is already taken /// token not found #[display(fmt = "Token not found. Is token registered?")] TokenNotFound, @@ -116,6 +115,10 @@ pub enum ServiceError { #[display(fmt = "{}", _0)] DBError(DBErrorWrapper), + + /// captcha not found + #[display(fmt = "Captcha not found.")] + CaptchaNotFound, } #[derive(Serialize, Deserialize)] @@ -175,6 +178,7 @@ impl ResponseError for ServiceError { } ServiceError::DBError(_) => StatusCode::INTERNAL_SERVER_ERROR, + ServiceError::CaptchaNotFound => StatusCode::NOT_FOUND, } } } @@ -197,10 +201,13 @@ impl From for ServiceError { impl From for ServiceError { #[cfg(not(tarpaulin_include))] fn from(e: DBError) -> ServiceError { + println!("from conversin: {}", e); match e { DBError::UsernameTaken => ServiceError::UsernameTaken, DBError::SecretTaken => ServiceError::InternalServerError, DBError::EmailTaken => ServiceError::EmailTaken, + DBError::AccountNotFound => ServiceError::AccountNotFound, + DBError::CaptchaNotFound => ServiceError::CaptchaNotFound, _ => ServiceError::DBError(DBErrorWrapper(e)), } }