增加校验错误信息处理

This commit is contained in:
李运家 2024-06-13 15:20:43 +08:00
parent b0e6948560
commit a04c65848a
15 changed files with 102 additions and 32 deletions

View File

@ -1,19 +1,18 @@
use axum::{Extension, Json};
use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword, RefreshToken}, vo::account::{LoginAccount, RefreshTokenResult}};
use library::{cache::account_cache::CacheAccount, resp::response::{ ResData, ResResult}};
use validator::Validate;
use library::{cache::account_cache::CacheAccount, res::{response::{ ResData, ResResult}, validator}};
pub async fn authenticate_google(
Json(req): Json<AuthenticateGooleAccountReq>
) -> ResResult<ResData<LoginAccount>> {
req.validate()?;
validator::validate_params(&req, "")?;
service::account::authenticate_google(req).await
}
pub async fn authenticate_with_password(
Json(req): Json<AuthenticateWithPassword>
) -> ResResult<ResData<LoginAccount>> {
req.validate()?;
validator::validate_params(&req, "")?;
service::sys_account::authenticate_with_password(req).await
}
@ -22,6 +21,6 @@ pub async fn refresh_token(
Json(refresh_token): Json<RefreshToken>
) -> ResResult<ResData<RefreshTokenResult>> {
tracing::debug!("刷新token, {:?}", account);
refresh_token.validate()?;
validator::validate_params(&refresh_token, "")?;
service::account::refresh_token(account, refresh_token.token).await
}

View File

@ -4,8 +4,8 @@ use validator::Validate;
use domain::dto::feedback::FeedbackAdd;
use domain::dto::pageable::PageParams;
use domain::entities::feedback::Feedback;
use library::resp::pageable::Pageable;
use library::resp::response::{ResData, ResResult};
use library::res::pageable::Pageable;
use library::res::response::{ResData, ResResult};
/// 添加反馈信息
pub async fn add_feedback(

View File

@ -1,7 +1,7 @@
extern crate self as library;
pub mod core;
pub mod resp;
pub mod res;
pub mod middleware;
pub mod token;
pub mod social;

View File

@ -1,4 +1,4 @@
use crate::resp::response::ResErr;
use crate::res::response::ResErr;
use axum::body::Body;
use axum::extract::Request;
use axum::http::header::CONTENT_TYPE;

4
library/src/res/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod response;
pub mod status;
pub mod pageable;
pub mod validator;

View File

@ -24,7 +24,7 @@ where
}
}
impl <T: Serialize> ResData<T> {
impl<T: Serialize> ResData<T> {
pub fn none() -> Self {
ResData(None)
}
@ -45,7 +45,7 @@ pub enum ResErr {
ErrService(Option<String>),
ErrSocial(Option<String>),
ErrAccountDisable(Option<String>),
ErrSqlx(Option<String>)
ErrSqlx(Option<String>),
}
use ResErr::*;
@ -189,42 +189,68 @@ impl Default for ResErr {
}
impl ResErr {
pub fn error<T>(code: i32, msg: T) -> Self where T: Into<String> {
pub fn error<T>(code: i32, msg: T) -> Self
where
T: Into<String>,
{
Error(code, msg.into())
}
pub fn params<T>(msg: T) -> Self where T: Into<String> {
pub fn params<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrParams(Some(msg.into()))
}
pub fn auth<T>(msg: T) -> Self where T: Into<String> {
pub fn auth<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrAuth(Some(msg.into()))
}
pub fn perm<T>(msg: T) -> Self where T: Into<String> {
pub fn perm<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrPerm(Some(msg.into()))
}
pub fn not_found<T>(msg: T) -> Self where T: Into<String> {
pub fn not_found<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrNotFound(Some(msg.into()))
}
pub fn system<T>(msg: T) -> Self where T: Into<String> {
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> {
pub fn data<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrData(Some(msg.into()))
}
pub fn service<T>(msg: T) -> Self where T: Into<String> {
pub fn service<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrService(Some(msg.into()))
}
pub fn social<T>(msg: T) -> Self where T: Into<String> {
pub fn social<T>(msg: T) -> Self
where
T: Into<String>,
{
ErrSocial(Some(msg.into()))
}
}
pub type ResResult<T> = std::result::Result<T, ResErr>;

View File

@ -0,0 +1,44 @@
use std::borrow::Cow;
use validator::Validate;
use super::response::{ResData, ResErr, ResResult};
pub fn validate_params(params: &impl Validate, _local: &str) -> ResResult<ResData<()>> {
params.validate().map_err(|e| {
let mut errors = vec![];
for (_, err) in e.errors().iter() {
match err {
validator::ValidationErrorsKind::Struct(err) => {
err.field_errors().iter().for_each(|(_field, errs)| {
errs.iter().for_each(|e| {
if let Some(Cow::Owned(msg)) = e.message.clone() {
errors.push(msg);
}
});
});
},
validator::ValidationErrorsKind::List(err) => {
for (_, err) in err.iter() {
err.field_errors().iter().for_each(|(_field, errs)| {
errs.iter().for_each(|e| {
if let Some(Cow::Owned(msg)) = e.message.clone() {
errors.push(msg);
}
});
});
}
},
validator::ValidationErrorsKind::Field(err) => {
err.iter().for_each(|e| {
if let Some(Cow::Owned(msg)) = e.message.clone() {
errors.push(msg);
}
});
},
};
}
ResErr::params(errors.join(";"))
})?;
Result::Ok(ResData::none())
}

View File

@ -1,3 +0,0 @@
pub mod response;
pub mod status;
pub mod pageable;

View File

@ -8,7 +8,7 @@ use reqwest::Client;
use serde::Deserialize;
use serde_json::Value;
use crate::resp::response::ResErr;
use crate::res::response::ResErr;
use super::SocialResult;

View File

@ -8,7 +8,7 @@ use reqwest::Client;
use serde::Deserialize;
use serde_json::Value;
use crate::resp::response::ResErr;
use crate::res::response::ResErr;
use super::SocialResult;

View File

@ -3,8 +3,8 @@ use domain::dto::account::AuthenticateGooleAccountReq;
use domain::entities::account::Account;
use domain::vo::account::{LoginAccount, RefreshTokenResult};
use library::cache::account_cache::{CacheAccount, LOGIN_CACHE};
use library::resp::response::ResErr::ErrPerm;
use library::resp::response::{ResData, ResErr, ResResult};
use library::res::response::ResErr::ErrPerm;
use library::res::response::{ResData, ResErr, ResResult};
use library::social::google::GOOGLE_SOCIAL;
use library::token::{generate_refresh_token, generate_token};
use library::{db, token};

View File

@ -1,8 +1,8 @@
use domain::dto::feedback::FeedbackAdd;
use domain::entities::feedback::Feedback;
use library::db;
use library::resp::pageable::Pageable;
use library::resp::response::{ResData, ResResult};
use library::res::pageable::Pageable;
use library::res::response::{ResData, ResResult};
/// 获取反馈信息列表
pub async fn get_feedback_list_by_page(page: i64, page_size: i64) -> ResResult<ResData<Pageable<Feedback>>> {
@ -37,7 +37,7 @@ pub async fn add_feedback(req: FeedbackAdd) -> ResResult<ResData<()>> {
}
Err(err) => {
tracing::error!(error = ?err, "添加反馈信息失败");
return Err(library::resp::response::ResErr::ErrSystem(None));
return Err(library::res::response::ResErr::ErrSystem(None));
}
}

View File

@ -4,7 +4,7 @@ use domain::{
entities::account::{Account, Role}, vo::account::LoginAccount,
};
use library::{
cache::account_cache::{CacheAccount, LOGIN_CACHE}, db, resp::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token}
cache::account_cache::{CacheAccount, LOGIN_CACHE}, db, res::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token}
};