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

View File

@ -12,7 +12,10 @@ use tower_http::classify::ServerErrorsFailureClass;
use tower_http::trace::TraceLayer;
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.
///
@ -27,11 +30,8 @@ pub async fn add_version(
) -> AppResult<impl IntoResponse> {
let mut res = next.run(req).await;
let headers = res.headers_mut();
headers.append("Server", HeaderValue::from_static(env!("CARGO_PKG_NAME")));
headers.append(
"Phthonus-Version",
HeaderValue::from_static(env!("CARGO_PKG_VERSION")),
);
headers.append("Server", HeaderValue::from_static(NAME));
headers.append("Phthonus-Version", HeaderValue::from_static(VERSION));
Ok(res)
}