feat: implement accountnotfound and captcha notfound err vals for sqlx postgres

This commit is contained in:
realaravinth 2022-05-13 19:08:31 +05:30
parent 3edb2252af
commit ddb6d336f7
No known key found for this signature in database
GPG Key ID: AD9F0F08E855ED88
2 changed files with 17 additions and 1 deletions

View File

@ -21,6 +21,15 @@ use std::borrow::Cow;
use db_core::dev::*; use db_core::dev::*;
use sqlx::Error; 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 /// map postgres errors to [DBError](DBError) types
pub fn map_register_err(e: Error) -> DBError { pub fn map_register_err(e: Error) -> DBError {
if let Error::Database(err) = e { if let Error::Database(err) = e {

View File

@ -106,7 +106,6 @@ pub enum ServiceError {
#[display(fmt = "Unable to send email, contact admin")] #[display(fmt = "Unable to send email, contact admin")]
UnableToSendEmail(SmtpErrorWrapper), UnableToSendEmail(SmtpErrorWrapper),
/// when the a token name is already taken
/// token not found /// token not found
#[display(fmt = "Token not found. Is token registered?")] #[display(fmt = "Token not found. Is token registered?")]
TokenNotFound, TokenNotFound,
@ -116,6 +115,10 @@ pub enum ServiceError {
#[display(fmt = "{}", _0)] #[display(fmt = "{}", _0)]
DBError(DBErrorWrapper), DBError(DBErrorWrapper),
/// captcha not found
#[display(fmt = "Captcha not found.")]
CaptchaNotFound,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -175,6 +178,7 @@ impl ResponseError for ServiceError {
} }
ServiceError::DBError(_) => StatusCode::INTERNAL_SERVER_ERROR, ServiceError::DBError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::CaptchaNotFound => StatusCode::NOT_FOUND,
} }
} }
} }
@ -197,10 +201,13 @@ impl From<CredsError> for ServiceError {
impl From<DBError> for ServiceError { impl From<DBError> for ServiceError {
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
fn from(e: DBError) -> ServiceError { fn from(e: DBError) -> ServiceError {
println!("from conversin: {}", e);
match e { match e {
DBError::UsernameTaken => ServiceError::UsernameTaken, DBError::UsernameTaken => ServiceError::UsernameTaken,
DBError::SecretTaken => ServiceError::InternalServerError, DBError::SecretTaken => ServiceError::InternalServerError,
DBError::EmailTaken => ServiceError::EmailTaken, DBError::EmailTaken => ServiceError::EmailTaken,
DBError::AccountNotFound => ServiceError::AccountNotFound,
DBError::CaptchaNotFound => ServiceError::CaptchaNotFound,
_ => ServiceError::DBError(DBErrorWrapper(e)), _ => ServiceError::DBError(DBErrorWrapper(e)),
} }
} }