优化错误消息相关逻辑
This commit is contained in:
parent
8cfcea719a
commit
76ae54cd9b
@ -4,6 +4,7 @@ use axum::{
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use http::StatusCode;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::status::Status;
|
||||
@ -40,8 +41,6 @@ pub enum ResErr {
|
||||
ErrAuth(Option<String>),
|
||||
ErrPerm(Option<String>),
|
||||
ErrNotFound(Option<String>),
|
||||
ErrSystem(Option<String>),
|
||||
ErrData(Option<String>),
|
||||
ErrService(Option<String>),
|
||||
ErrSocial(Option<String>),
|
||||
ErrAccountDisable(Option<String>),
|
||||
@ -53,82 +52,66 @@ use ResErr::*;
|
||||
impl IntoResponse for ResErr {
|
||||
fn into_response(self) -> Response {
|
||||
let status = match self {
|
||||
Error(code, msg) => Status::<()>::Err(code, msg),
|
||||
Error(code, msg) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, msg),
|
||||
ErrParams(msg) => {
|
||||
let code = 10000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("参数错误")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("参数错误")),
|
||||
}
|
||||
}
|
||||
ErrAuth(msg) => {
|
||||
let code = 20000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("未授权,请先登录")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, v),
|
||||
None => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, String::from("未授权,请先登录")),
|
||||
}
|
||||
}
|
||||
ErrPerm(msg) => {
|
||||
let code = 30000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("权限不足")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v),
|
||||
None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("权限不足")),
|
||||
}
|
||||
}
|
||||
ErrNotFound(msg) => {
|
||||
let code = 40000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("数据不存在")),
|
||||
}
|
||||
}
|
||||
ErrSystem(msg) => {
|
||||
let code = 50000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("内部服务器错误,请稍后重试")),
|
||||
}
|
||||
}
|
||||
ErrData(msg) => {
|
||||
let code = 60000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("数据异常")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::NOT_FOUND, code, v),
|
||||
None => Status::<()>::Err(StatusCode::NOT_FOUND, code, String::from("数据不存在")),
|
||||
}
|
||||
}
|
||||
ErrService(msg) => {
|
||||
let code = 70000;
|
||||
let code = 50000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("服务异常")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("内部服务器错误,请稍后重试")),
|
||||
}
|
||||
}
|
||||
ErrSocial(msg) => {
|
||||
let code = 80000;
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("社交业务异常")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("社交业务异常")),
|
||||
}
|
||||
}
|
||||
ErrAccountDisable(msg) => {
|
||||
let code = 90000;
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("账户已禁用")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v),
|
||||
None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("账户已禁用")),
|
||||
}
|
||||
}
|
||||
ErrSqlx(msg) => {
|
||||
let code = 90000;
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(code, v),
|
||||
None => Status::<()>::Err(code, String::from("账户已禁用")),
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("业务错误")),
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -146,8 +129,6 @@ impl Display for ResErr {
|
||||
| ErrAuth(message)
|
||||
| ErrPerm(message)
|
||||
| ErrNotFound(message)
|
||||
| ErrSystem(message)
|
||||
| ErrData(message)
|
||||
| ErrService(message)
|
||||
| ErrSocial(message)
|
||||
| ErrAccountDisable(message)
|
||||
@ -160,7 +141,7 @@ impl StdError for ResErr {}
|
||||
|
||||
impl From<String> for ResErr {
|
||||
fn from(value: String) -> Self {
|
||||
ErrSystem(Some(value))
|
||||
ErrService(Some(value))
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,11 +165,12 @@ impl From<validator::ValidationErrors> for ResErr {
|
||||
|
||||
impl Default for ResErr {
|
||||
fn default() -> Self {
|
||||
ErrSystem(None)
|
||||
ErrService(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResErr {
|
||||
#[inline]
|
||||
pub fn error<T>(code: i32, msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
@ -196,6 +178,7 @@ impl ResErr {
|
||||
Error(code, msg.into())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn params<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
@ -203,6 +186,7 @@ impl ResErr {
|
||||
ErrParams(Some(msg.into()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn auth<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
@ -210,6 +194,7 @@ impl ResErr {
|
||||
ErrAuth(Some(msg.into()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn perm<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
@ -217,6 +202,7 @@ impl ResErr {
|
||||
ErrPerm(Some(msg.into()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn not_found<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
@ -224,20 +210,7 @@ impl ResErr {
|
||||
ErrNotFound(Some(msg.into()))
|
||||
}
|
||||
|
||||
pub fn system<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
{
|
||||
ErrSystem(Some(msg.into()))
|
||||
}
|
||||
|
||||
pub fn data<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
{
|
||||
ErrData(Some(msg.into()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn service<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
@ -245,6 +218,7 @@ impl ResErr {
|
||||
ErrService(Some(msg.into()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn social<T>(msg: T) -> Self
|
||||
where
|
||||
T: Into<String>,
|
||||
|
@ -1,3 +1,4 @@
|
||||
use http::StatusCode;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
@ -5,6 +6,8 @@ pub struct Reply<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[serde(skip_serializing)]
|
||||
pub status: StatusCode,
|
||||
pub code: i32,
|
||||
pub err: bool,
|
||||
pub msg: String,
|
||||
@ -17,7 +20,7 @@ where
|
||||
T: Serialize,
|
||||
{
|
||||
OK(Option<T>),
|
||||
Err(i32, String),
|
||||
Err(StatusCode, i32, String),
|
||||
}
|
||||
|
||||
impl<T> Status<T>
|
||||
@ -26,6 +29,7 @@ where
|
||||
{
|
||||
pub fn to_reply(self) -> Reply<T> {
|
||||
let mut resp = Reply {
|
||||
status: StatusCode::OK,
|
||||
code: 0,
|
||||
err: false,
|
||||
msg: String::from("OK"),
|
||||
@ -36,7 +40,8 @@ where
|
||||
Status::OK(data) => {
|
||||
resp.data = data;
|
||||
}
|
||||
Status::Err(code, msg) => {
|
||||
Status::Err(status, code, msg) => {
|
||||
resp.status = status;
|
||||
resp.code = code;
|
||||
resp.err = true;
|
||||
resp.msg = msg;
|
||||
|
@ -5,6 +5,7 @@ use library::{context::{Context, WhiteContext}, res::{response::{ ResData, ResRe
|
||||
use crate::service;
|
||||
|
||||
/// google账号登录
|
||||
/// post: /account/google
|
||||
pub async fn authenticate_google(
|
||||
Extension(context): Extension<WhiteContext>,
|
||||
Json(req): Json<AuthenticateGooleAccountReq>
|
||||
|
@ -1,9 +1,9 @@
|
||||
use axum::extract::Query;
|
||||
use axum::{Extension, Json};
|
||||
use library::context::Context;
|
||||
use domain::dto::feedback::FeedbackAdd;
|
||||
use domain::dto::pageable::PageParams;
|
||||
use domain::entities::feedback::Feedback;
|
||||
use library::context::Context;
|
||||
use library::res::pageable::Pageable;
|
||||
use library::res::response::{ResData, ResResult};
|
||||
use library::res::validator;
|
||||
@ -13,7 +13,7 @@ use crate::service;
|
||||
/// 添加反馈信息
|
||||
pub async fn add_feedback(
|
||||
Extension(context): Extension<Context>,
|
||||
Json(req): Json<FeedbackAdd>
|
||||
Json(req): Json<FeedbackAdd>,
|
||||
) -> ResResult<ResData<()>> {
|
||||
validator::validate_params(&req, context.get_lang_tag())?;
|
||||
service::feedback_service::add_feedback(context, req).await
|
||||
@ -22,7 +22,13 @@ pub async fn add_feedback(
|
||||
/// 获取反馈信息列表
|
||||
pub async fn get_feedback_list_by_page(
|
||||
Extension(context): Extension<Context>,
|
||||
Query(page_params): Query<PageParams>
|
||||
Query(page_params): Query<PageParams>,
|
||||
) -> ResResult<ResData<Pageable<Feedback>>> {
|
||||
service::feedback_service::get_feedback_list_by_page(context, page_params.page.unwrap(), page_params.page_size.unwrap()).await
|
||||
}
|
||||
validator::validate_params(&page_params, context.get_lang_tag())?;
|
||||
service::feedback_service::get_feedback_list_by_page(
|
||||
context,
|
||||
page_params.page.unwrap(),
|
||||
page_params.page_size.unwrap(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ pub async fn authenticate_google(
|
||||
if let Some(disable_time) = account.disable_time {
|
||||
if disable_time > Utc::now() {
|
||||
tracing::error!("账户已禁用");
|
||||
return Err(ResErr::system(message!(context.get_lang_tag(), ACCOUNT_DISABLED)));
|
||||
return Err(ResErr::service(message!(context.get_lang_tag(), ACCOUNT_DISABLED)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ pub async fn add_feedback(
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::error!(error = ?err, "添加反馈信息失败");
|
||||
return Err(library::res::response::ResErr::ErrSystem(None));
|
||||
return Err(library::res::response::ResErr::ErrService(None));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user