feat(axum): update unused constants

This commit is contained in:
xfy
2024-09-24 09:14:39 +08:00
parent d6eeab3b3c
commit 601bfd9da7
2 changed files with 15 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use std::{borrow::Cow, fmt::Display}; use std::fmt::Display;
use axum::{ use axum::{
extract::rejection::{FormRejection, JsonRejection}, extract::rejection::{FormRejection, JsonRejection},
@ -20,13 +20,12 @@ pub enum AppError {
AxumFormRejection(#[from] FormRejection), AxumFormRejection(#[from] FormRejection),
#[error(transparent)] #[error(transparent)]
AxumJsonRejection(#[from] JsonRejection), AxumJsonRejection(#[from] JsonRejection),
// route // route
// 路由通常错误 错误信息直接返回用户 // 路由通常错误 错误信息直接返回用户
#[error("{0}")] // #[error("{0}")]
AuthorizeFailed(Cow<'static, str>), // AuthorizeFailed(Cow<'static, str>),
#[error("{0}")] // #[error("{0}")]
UserConflict(Cow<'static, str>), // UserConflict(Cow<'static, str>),
} }
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
@ -81,12 +80,11 @@ impl IntoResponse for AppError {
ParameterIncorrect, ParameterIncorrect,
self.to_string(), self.to_string(),
), ),
// route // route
AppError::AuthorizeFailed(err) => { // AppError::AuthorizeFailed(err) => {
(StatusCode::UNAUTHORIZED, AuthorizeFailed, err.to_string()) // (StatusCode::UNAUTHORIZED, AuthorizeFailed, err.to_string())
} // }
AppError::UserConflict(err) => (StatusCode::CONFLICT, UserConflict, err.to_string()), // AppError::UserConflict(err) => (StatusCode::CONFLICT, UserConflict, err.to_string()),
}; };
let body = Json(json!({ let body = Json(json!({
"code": code, "code": code,

View File

@ -12,7 +12,10 @@ use tower_http::classify::ServerErrorsFailureClass;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::{error, info, info_span, Span}; use tracing::{error, info, info_span, Span};
use crate::error::AppResult; use crate::{
consts::{NAME, VERSION},
error::AppResult,
};
/// Middleware for adding version information to each response's headers. /// Middleware for adding version information to each response's headers.
/// ///
@ -27,11 +30,8 @@ pub async fn add_version(
) -> AppResult<impl IntoResponse> { ) -> AppResult<impl IntoResponse> {
let mut res = next.run(req).await; let mut res = next.run(req).await;
let headers = res.headers_mut(); let headers = res.headers_mut();
headers.append("Server", HeaderValue::from_static(env!("CARGO_PKG_NAME"))); headers.append("Server", HeaderValue::from_static(NAME));
headers.append( headers.append("Phthonus-Version", HeaderValue::from_static(VERSION));
"Phthonus-Version",
HeaderValue::from_static(env!("CARGO_PKG_VERSION")),
);
Ok(res) Ok(res)
} }