use axum::{http::StatusCode, response::IntoResponse, Json}; use serde_json::json; #[derive(Debug)] pub enum AuthError { InternalError(E), InternalE, MissingCredentials, InvalidCredentials, MissingToken, InvalidToken, MissingUser, } impl IntoResponse for AuthError { fn into_response(self) -> axum::response::Response { let (status, message) = match self { Self::InternalError(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Internal Error: {}", e.to_string())), Self::InternalE => (StatusCode::INTERNAL_SERVER_ERROR, "Internal E".to_string()), Self::MissingCredentials => { (StatusCode::BAD_REQUEST, "Missing credentials".to_string()) } Self::InvalidCredentials => { (StatusCode::UNAUTHORIZED, "Invalid credentials".to_string()) } Self::MissingToken => (StatusCode::BAD_REQUEST, "Missing token".to_string()), Self::InvalidToken => (StatusCode::UNAUTHORIZED, "Invalid token".to_string()), Self::MissingUser => (StatusCode::UNAUTHORIZED, "User not exists".to_string()), }; ( status, Json(json!({ "status": status.to_string(), "message": message })), ) .into_response() } } pub fn internal_error(err: E) -> (StatusCode, Json) where E: std::error::Error, { ( StatusCode::INTERNAL_SERVER_ERROR, Json(json!({ "status": StatusCode::INTERNAL_SERVER_ERROR.to_string(), "message": err.to_string() })), ) }