actix_jwt_authc/
errors.rs

1use actix_web::http::StatusCode;
2use actix_web::{HttpResponse, ResponseError};
3use derive_more::{Display, Error};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Display, Error)]
7pub enum Error {
8    #[display(fmt = "Internal error")]
9    InternalError,
10    #[display(fmt = "Unauthenticated")]
11    Unauthenticated,
12    #[display(fmt = "Invalid session [{}]", _0)]
13    InvalidSession(#[error(not(source))] String),
14}
15
16#[derive(Debug, Serialize, Deserialize)]
17pub struct ErrorResponse {
18    pub message: String,
19}
20
21impl ResponseError for Error {
22    fn error_response(&self) -> HttpResponse {
23        let response = ErrorResponse {
24            message: self.to_string(),
25        };
26        HttpResponse::build(self.status_code()).json(response)
27    }
28
29    fn status_code(&self) -> StatusCode {
30        match *self {
31            Error::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
32            Error::Unauthenticated => StatusCode::UNAUTHORIZED,
33            Error::InvalidSession(_) => StatusCode::UNAUTHORIZED,
34        }
35    }
36}