Allow converting AuthenticationError to ApiError

This commit is contained in:
Xiretza 2024-02-10 12:23:20 +00:00
parent ac7f03db15
commit 578c4e237f

View file

@ -1,11 +1,14 @@
use axum::{
extract::Query,
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Form, Json, Router,
};
use serde::{Deserialize, Serialize};
use tracing::{event, instrument, Level};
use crate::auth::AuthenticationError;
use crate::secrets::Password;
#[instrument]
@ -21,11 +24,29 @@ pub async fn run() -> color_eyre::Result<()> {
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct ApiError {
#[serde(skip_serializing)]
status: StatusCode,
error: String,
message: String,
}
type ApiResult<T> = Result<T, Json<ApiError>>;
impl From<AuthenticationError> for ApiError {
fn from(err: AuthenticationError) -> Self {
Self {
status: StatusCode::UNAUTHORIZED,
error: "authentication-failed".to_owned(),
message: err.to_string(),
}
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> axum::response::Response {
(self.status, Json(self)).into_response()
}
}
type ApiResult<T> = Result<T, ApiError>;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
struct ApiVersion {