use actix_web::{error::ResponseError, HttpResponse}; use argonautica::Error as HashError; use derive_more::Display; use diesel::result::Error as DieselError; use log::error; use std::convert::From; #[derive(Debug, Display)] pub enum ServiceError { #[display(fmt = "Internal Server Error")] InternalServerError, #[display(fmt = "Unauthorized")] Unauthorized, #[display(fmt = "Not Found")] NotFound, } impl ResponseError for ServiceError { fn error_response(&self) -> HttpResponse { match *self { ServiceError::InternalServerError => { HttpResponse::InternalServerError().body("Internal Server Error") } ServiceError::Unauthorized => HttpResponse::Unauthorized().body("Unauthorized"), ServiceError::NotFound => HttpResponse::NotFound().body("404 Not Found"), } } } impl From for ServiceError { fn from(e: DieselError) -> ServiceError { error!("Database error: {}", e); ServiceError::InternalServerError } } impl From for ServiceError { fn from(e: HashError) -> ServiceError { error!("Hash error: {}", e); ServiceError::InternalServerError } }