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
|
use axum::{Json, body::Body, http::{Response, StatusCode}, response::IntoResponse};
use serde_json::{json};
pub enum RouteError {
Internal(String),
UserCreateEmailExists(String),
UserCreateUsernameExists(String),
UnregisteredEmail(String),
AuthorizationFailure(),
}
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))
},
RouteError::UnregisteredEmail(email) => {
(StatusCode::BAD_REQUEST, format!("email \"{}\" is not registered", email))
},
RouteError::AuthorizationFailure() => {
(StatusCode::UNAUTHORIZED, format!("failed to authorize"))
}
};
(status, Json(json!({"error": message}))).into_response()
}
}
|