summaryrefslogtreecommitdiff
path: root/src/routes/errors.rs
blob: 6261d75507bb0f86df2e6e6502f0946135965728 (plain)
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
use axum::{Json, body::Body, http::{Response, StatusCode}, response::IntoResponse};
use serde_json::{json};

pub enum RouteError {
    Internal(String),
    UserCreateEmailExists(String),
    UserCreateUsernameExists(String),
}

impl IntoResponse for RouteError {
    fn into_response(self) -> Response<Body> {
        let (status, message) = match self {
            Self::Internal(message) => {
                (StatusCode::INTERNAL_SERVER_ERROR, message)
            },
            RouteError::UserCreateEmailExists(email) => {
                (StatusCode::BAD_REQUEST, format!("user with email \"{}\" already exists", email))
            },
            RouteError::UserCreateUsernameExists(username) => {
                (StatusCode::BAD_REQUEST, format!("user with username \"{}\" already exists", username))
            }
        };

        (status, Json(json!({"error": message}))).into_response()
    }
}