优化错误消息相关逻辑

This commit is contained in:
李运家 2024-09-23 20:08:43 +08:00
parent 8cfcea719a
commit 76ae54cd9b
6 changed files with 49 additions and 63 deletions

View File

@ -4,6 +4,7 @@ use axum::{
response::{IntoResponse, Response}, response::{IntoResponse, Response},
Json, Json,
}; };
use http::StatusCode;
use serde::Serialize; use serde::Serialize;
use super::status::Status; use super::status::Status;
@ -40,8 +41,6 @@ pub enum ResErr {
ErrAuth(Option<String>), ErrAuth(Option<String>),
ErrPerm(Option<String>), ErrPerm(Option<String>),
ErrNotFound(Option<String>), ErrNotFound(Option<String>),
ErrSystem(Option<String>),
ErrData(Option<String>),
ErrService(Option<String>), ErrService(Option<String>),
ErrSocial(Option<String>), ErrSocial(Option<String>),
ErrAccountDisable(Option<String>), ErrAccountDisable(Option<String>),
@ -53,82 +52,66 @@ use ResErr::*;
impl IntoResponse for ResErr { impl IntoResponse for ResErr {
fn into_response(self) -> Response { fn into_response(self) -> Response {
let status = match self { let status = match self {
Error(code, msg) => Status::<()>::Err(code, msg), Error(code, msg) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, msg),
ErrParams(msg) => { ErrParams(msg) => {
let code = 10000; let code = 10000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
None => Status::<()>::Err(code, String::from("参数错误")), None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("参数错误")),
} }
} }
ErrAuth(msg) => { ErrAuth(msg) => {
let code = 20000; let code = 20000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, v),
None => Status::<()>::Err(code, String::from("未授权,请先登录")), None => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, String::from("未授权,请先登录")),
} }
} }
ErrPerm(msg) => { ErrPerm(msg) => {
let code = 30000; let code = 30000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v),
None => Status::<()>::Err(code, String::from("权限不足")), None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("权限不足")),
} }
} }
ErrNotFound(msg) => { ErrNotFound(msg) => {
let code = 40000; let code = 40000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::NOT_FOUND, code, v),
None => Status::<()>::Err(code, String::from("数据不存在")), None => Status::<()>::Err(StatusCode::NOT_FOUND, 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("数据异常")),
} }
} }
ErrService(msg) => { ErrService(msg) => {
let code = 70000; let code = 50000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
None => Status::<()>::Err(code, String::from("服务异常")), None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("内部服务器错误,请稍后重试")),
} }
} }
ErrSocial(msg) => { ErrSocial(msg) => {
let code = 80000; let code = 80000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
None => Status::<()>::Err(code, String::from("社交业务异常")), None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("社交业务异常")),
} }
} }
ErrAccountDisable(msg) => { ErrAccountDisable(msg) => {
let code = 90000; let code = 90000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v),
None => Status::<()>::Err(code, String::from("账户已禁用")), None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("账户已禁用")),
} }
} }
ErrSqlx(msg) => { ErrSqlx(msg) => {
let code = 90000; let code = 90000;
match msg { match msg {
Some(v) => Status::<()>::Err(code, v), Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
None => Status::<()>::Err(code, String::from("账户已禁用")), None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("业务错误")),
} }
} }
}; };
@ -146,8 +129,6 @@ impl Display for ResErr {
| ErrAuth(message) | ErrAuth(message)
| ErrPerm(message) | ErrPerm(message)
| ErrNotFound(message) | ErrNotFound(message)
| ErrSystem(message)
| ErrData(message)
| ErrService(message) | ErrService(message)
| ErrSocial(message) | ErrSocial(message)
| ErrAccountDisable(message) | ErrAccountDisable(message)
@ -160,7 +141,7 @@ impl StdError for ResErr {}
impl From<String> for ResErr { impl From<String> for ResErr {
fn from(value: String) -> Self { 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 { impl Default for ResErr {
fn default() -> Self { fn default() -> Self {
ErrSystem(None) ErrService(None)
} }
} }
impl ResErr { impl ResErr {
#[inline]
pub fn error<T>(code: i32, msg: T) -> Self pub fn error<T>(code: i32, msg: T) -> Self
where where
T: Into<String>, T: Into<String>,
@ -196,6 +178,7 @@ impl ResErr {
Error(code, msg.into()) Error(code, msg.into())
} }
#[inline]
pub fn params<T>(msg: T) -> Self pub fn params<T>(msg: T) -> Self
where where
T: Into<String>, T: Into<String>,
@ -203,6 +186,7 @@ impl ResErr {
ErrParams(Some(msg.into())) ErrParams(Some(msg.into()))
} }
#[inline]
pub fn auth<T>(msg: T) -> Self pub fn auth<T>(msg: T) -> Self
where where
T: Into<String>, T: Into<String>,
@ -210,6 +194,7 @@ impl ResErr {
ErrAuth(Some(msg.into())) ErrAuth(Some(msg.into()))
} }
#[inline]
pub fn perm<T>(msg: T) -> Self pub fn perm<T>(msg: T) -> Self
where where
T: Into<String>, T: Into<String>,
@ -217,6 +202,7 @@ impl ResErr {
ErrPerm(Some(msg.into())) ErrPerm(Some(msg.into()))
} }
#[inline]
pub fn not_found<T>(msg: T) -> Self pub fn not_found<T>(msg: T) -> Self
where where
T: Into<String>, T: Into<String>,
@ -224,20 +210,7 @@ impl ResErr {
ErrNotFound(Some(msg.into())) ErrNotFound(Some(msg.into()))
} }
pub fn system<T>(msg: T) -> Self #[inline]
where
T: Into<String>,
{
ErrSystem(Some(msg.into()))
}
pub fn data<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrData(Some(msg.into()))
}
pub fn service<T>(msg: T) -> Self pub fn service<T>(msg: T) -> Self
where where
T: Into<String>, T: Into<String>,
@ -245,6 +218,7 @@ impl ResErr {
ErrService(Some(msg.into())) ErrService(Some(msg.into()))
} }
#[inline]
pub fn social<T>(msg: T) -> Self pub fn social<T>(msg: T) -> Self
where where
T: Into<String>, T: Into<String>,

View File

@ -1,3 +1,4 @@
use http::StatusCode;
use serde::Serialize; use serde::Serialize;
#[derive(Serialize)] #[derive(Serialize)]
@ -5,6 +6,8 @@ pub struct Reply<T>
where where
T: Serialize, T: Serialize,
{ {
#[serde(skip_serializing)]
pub status: StatusCode,
pub code: i32, pub code: i32,
pub err: bool, pub err: bool,
pub msg: String, pub msg: String,
@ -17,7 +20,7 @@ where
T: Serialize, T: Serialize,
{ {
OK(Option<T>), OK(Option<T>),
Err(i32, String), Err(StatusCode, i32, String),
} }
impl<T> Status<T> impl<T> Status<T>
@ -26,6 +29,7 @@ where
{ {
pub fn to_reply(self) -> Reply<T> { pub fn to_reply(self) -> Reply<T> {
let mut resp = Reply { let mut resp = Reply {
status: StatusCode::OK,
code: 0, code: 0,
err: false, err: false,
msg: String::from("OK"), msg: String::from("OK"),
@ -36,7 +40,8 @@ where
Status::OK(data) => { Status::OK(data) => {
resp.data = data; resp.data = data;
} }
Status::Err(code, msg) => { Status::Err(status, code, msg) => {
resp.status = status;
resp.code = code; resp.code = code;
resp.err = true; resp.err = true;
resp.msg = msg; resp.msg = msg;

View File

@ -5,6 +5,7 @@ use library::{context::{Context, WhiteContext}, res::{response::{ ResData, ResRe
use crate::service; use crate::service;
/// google账号登录 /// google账号登录
/// post: /account/google
pub async fn authenticate_google( pub async fn authenticate_google(
Extension(context): Extension<WhiteContext>, Extension(context): Extension<WhiteContext>,
Json(req): Json<AuthenticateGooleAccountReq> Json(req): Json<AuthenticateGooleAccountReq>

View File

@ -1,9 +1,9 @@
use axum::extract::Query; use axum::extract::Query;
use axum::{Extension, Json}; use axum::{Extension, Json};
use library::context::Context;
use domain::dto::feedback::FeedbackAdd; use domain::dto::feedback::FeedbackAdd;
use domain::dto::pageable::PageParams; use domain::dto::pageable::PageParams;
use domain::entities::feedback::Feedback; use domain::entities::feedback::Feedback;
use library::context::Context;
use library::res::pageable::Pageable; use library::res::pageable::Pageable;
use library::res::response::{ResData, ResResult}; use library::res::response::{ResData, ResResult};
use library::res::validator; use library::res::validator;
@ -13,7 +13,7 @@ use crate::service;
/// 添加反馈信息 /// 添加反馈信息
pub async fn add_feedback( pub async fn add_feedback(
Extension(context): Extension<Context>, Extension(context): Extension<Context>,
Json(req): Json<FeedbackAdd> Json(req): Json<FeedbackAdd>,
) -> ResResult<ResData<()>> { ) -> ResResult<ResData<()>> {
validator::validate_params(&req, context.get_lang_tag())?; validator::validate_params(&req, context.get_lang_tag())?;
service::feedback_service::add_feedback(context, req).await 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( pub async fn get_feedback_list_by_page(
Extension(context): Extension<Context>, Extension(context): Extension<Context>,
Query(page_params): Query<PageParams> Query(page_params): Query<PageParams>,
) -> ResResult<ResData<Pageable<Feedback>>> { ) -> 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
}

View File

@ -48,7 +48,7 @@ pub async fn authenticate_google(
if let Some(disable_time) = account.disable_time { if let Some(disable_time) = account.disable_time {
if disable_time > Utc::now() { if disable_time > Utc::now() {
tracing::error!("账户已禁用"); tracing::error!("账户已禁用");
return Err(ResErr::system(message!(context.get_lang_tag(), ACCOUNT_DISABLED))); return Err(ResErr::service(message!(context.get_lang_tag(), ACCOUNT_DISABLED)));
} }
} }

View File

@ -50,7 +50,7 @@ pub async fn add_feedback(
} }
Err(err) => { Err(err) => {
tracing::error!(error = ?err, "添加反馈信息失败"); tracing::error!(error = ?err, "添加反馈信息失败");
return Err(library::res::response::ResErr::ErrSystem(None)); return Err(library::res::response::ResErr::ErrService(None));
} }
} }