diff options
| author | Jokler <jokler@protonmail.com> | 2020-06-21 06:37:46 +0200 |
|---|---|---|
| committer | Jokler <jokler@protonmail.com> | 2020-06-21 06:37:46 +0200 |
| commit | e6468b012d5b33dd16992652da57f11dd5a6e82f (patch) | |
| tree | e89add440df79d4036b9b44d8c77ee6d69e67201 /src/error.rs | |
| download | joklerpoints-e6468b012d5b33dd16992652da57f11dd5a6e82f.tar.gz joklerpoints-e6468b012d5b33dd16992652da57f11dd5a6e82f.zip | |
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..062bb3d --- /dev/null +++ b/src/error.rs @@ -0,0 +1,44 @@ +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<DieselError> for ServiceError { + fn from(e: DieselError) -> ServiceError { + error!("Database error: {}", e); + ServiceError::InternalServerError + } +} + +impl From<HashError> for ServiceError { + fn from(e: HashError) -> ServiceError { + error!("Hash error: {}", e); + ServiceError::InternalServerError + } +} |
