summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 062bb3d21ea396f553d33c238e89684b6e0232e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
    }
}