mirror of
https://github.com/DefectingCat/phthonus
synced 2025-07-16 00:51:31 +00:00
feat(axum): update unused constants
This commit is contained in:
@ -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,
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user