mahjong_service/
service_error.rs

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
45
use actix_web::{body::BoxBody, http::StatusCode, HttpResponse, ResponseError};
use std::fmt::{self, Display};

#[derive(Debug)]
pub enum ServiceError {
    GameNotFound,
    GameVersionMismatch,
    ErrorLoadingGame,
    Custom(&'static str),
}

impl Display for ServiceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::GameNotFound => write!(f, "Game not found"),
            Self::GameVersionMismatch => write!(f, "Game version mismatch"),
            Self::ErrorLoadingGame => write!(f, "Error loading game"),
            Self::Custom(msg) => write!(f, "{}", msg),
        }
    }
}

impl ResponseError for ServiceError {
    fn status_code(&self) -> StatusCode {
        match self {
            Self::GameNotFound => StatusCode::NOT_FOUND,
            Self::GameVersionMismatch => StatusCode::BAD_REQUEST,
            Self::ErrorLoadingGame => StatusCode::INTERNAL_SERVER_ERROR,
            Self::Custom(_) => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    fn error_response(&self) -> HttpResponse<BoxBody> {
        match self {
            Self::GameNotFound => HttpResponse::NotFound().body("Game not found"),
            Self::GameVersionMismatch => HttpResponse::BadRequest().body("Game version mismatch"),
            Self::ErrorLoadingGame => {
                HttpResponse::InternalServerError().body("Error loading game")
            }
            Self::Custom(msg) => HttpResponse::InternalServerError().body(msg.to_string()),
        }
    }
}

pub type ResponseCommon = actix_web::Result<HttpResponse>;