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(), NotFound(String), } impl IntoResponse for RouteError { fn into_response(self) -> Response { 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")) }, RouteError::NotFound(resource) => { (StatusCode::NOT_FOUND, format!("{resource} not found")) } }; (status, Json(json!({"error": message}))).into_response() } }