lacoctelera/domain/
error.rsuse actix_web::{http::StatusCode, HttpResponse, ResponseError};
use thiserror::Error;
use validator::ValidationErrors;
#[derive(Error, Debug)]
pub enum DataDomainError {
#[error("Some params contain an invalid format")]
InvalidParams {
#[from]
source: ValidationErrors,
},
#[error("The given Author ID hash an invalid format")]
InvalidId,
#[error("The given string is not a valid recipe's category")]
InvalidRecipeCategory,
#[error("The data provided in the form is invalid")]
InvalidFormData,
#[error("The search criteria is invalid")]
InvalidSearch,
#[error("Expired access token")]
ExpiredAccess,
#[error("Wrong access token")]
InvalidAccessCredentials,
#[error("Email not registered in the DB")]
InvalidEmail,
#[error("Account disabled")]
AccountDisabled,
#[error("Parsing error")]
InvalidData,
}
#[derive(Error, Debug)]
pub enum ServerError {
#[error("Error from a DB query")]
DbError,
#[error("Error from the email client")]
EmailClientError,
}
impl ResponseError for ServerError {
fn status_code(&self) -> actix_web::http::StatusCode {
match self {
ServerError::DbError => StatusCode::INTERNAL_SERVER_ERROR,
ServerError::EmailClientError => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
HttpResponse::InternalServerError().body(format!(
include_str!("../../static/message_template.html"),
"<h3>Detected an error in the server, please, try again later.</h3>"
))
}
}
impl ResponseError for DataDomainError {
fn status_code(&self) -> StatusCode {
match self {
DataDomainError::InvalidAccessCredentials => StatusCode::FORBIDDEN,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
HttpResponse::InternalServerError().body(format!(
include_str!("../../static/message_template.html"),
"<h3>Detected an error in the server, please, try again later.</h3>"
))
}
}