Add API error type

This commit is contained in:
Xiretza 2024-02-10 10:49:36 +00:00
parent 32e99b155c
commit 0006004db1

View file

@ -1,6 +1,5 @@
use axum::{ use axum::{
extract::Query, extract::Query,
response::ErrorResponse,
routing::{get, post}, routing::{get, post},
Form, Json, Router, Form, Json, Router,
}; };
@ -20,6 +19,14 @@ pub async fn run() -> color_eyre::Result<()> {
Ok(()) Ok(())
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct ApiError {
error: String,
message: String,
}
type ApiResult<T> = Result<T, Json<ApiError>>;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
struct ApiVersion { struct ApiVersion {
api_version: String, api_version: String,
@ -41,7 +48,7 @@ struct LoginResponse {
async fn api_login( async fn api_login(
Query(ApiVersion { api_version }): Query<ApiVersion>, Query(ApiVersion { api_version }): Query<ApiVersion>,
Form(LoginRequest { username, password }): Form<LoginRequest>, Form(LoginRequest { username, password }): Form<LoginRequest>,
) -> Result<Json<LoginResponse>, ErrorResponse> { ) -> ApiResult<Json<LoginResponse>> {
event!(Level::WARN, "Creating dummy token"); event!(Level::WARN, "Creating dummy token");
Ok(Json(LoginResponse { Ok(Json(LoginResponse {