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
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use derive_more::{Display, Error};
use serde::{Deserialize, Serialize};
#[derive(Debug, Display, Error)]
pub enum Error {
#[display(fmt = "Internal error")]
InternalError,
#[display(fmt = "Unauthenticated")]
Unauthenticated,
#[display(fmt = "Invalid session [{}]", _0)]
InvalidSession(#[error(not(source))] String),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse {
pub message: String,
}
impl ResponseError for Error {
fn error_response(&self) -> HttpResponse {
let response = ErrorResponse {
message: self.to_string(),
};
HttpResponse::build(self.status_code()).json(response)
}
fn status_code(&self) -> StatusCode {
match *self {
Error::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
Error::Unauthenticated => StatusCode::UNAUTHORIZED,
Error::InvalidSession(_) => StatusCode::UNAUTHORIZED,
}
}
}