From 7cb2f58e297a55e4a3b79c30ad82ccd038e36b22 Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Thu, 7 Dec 2023 20:46:06 +0800 Subject: [PATCH] update better erroe handle --- src/template/src/app_response.hbs | 83 ++++++++++++++++++++++++------- src/template/src/routers/user.hbs | 52 ++++++++----------- 2 files changed, 87 insertions(+), 48 deletions(-) diff --git a/src/template/src/app_response.hbs b/src/template/src/app_response.hbs index 309f3f9..fae9ef9 100644 --- a/src/template/src/app_response.hbs +++ b/src/template/src/app_response.hbs @@ -1,8 +1,6 @@ use salvo::{ - async_trait, - prelude::EndpointOutRegister, - writing::Json, - Depot, Request, Response, Writer, hyper::StatusCode, + async_trait, hyper::StatusCode, prelude::EndpointOutRegister, writing::Json, Depot, Request, + Response, Writer, }; use serde::Serialize; @@ -14,7 +12,7 @@ pub struct AppResponse(pub AppResult); impl Writer for AppResponse { async fn write(self, req: &mut Request, depot: &mut Depot, res: &mut Response) { match self.0 { - Ok(data) => Res::with_data(data).into_response(res), + Ok(data) => ResponseBuilder::with_data(data).into_response(res), Err(e) => e.write(req, depot, res).await, } } @@ -44,19 +42,21 @@ impl From for AppResponse { } #[derive(Debug, Serialize, Default)] -pub struct Res { +pub struct ResponseBuilder { pub code: i32, pub data: T, pub msg: String, } -#[derive(Debug, Serialize, Default)] -pub struct ErrRes { +#[derive(Debug, Serialize)] +pub struct ErrorResponseBuilder { pub code: i32, pub msg: String, + #[serde(skip)] + pub source_error: AppError, } -impl Res { +impl ResponseBuilder { pub fn with_data(data: T) -> Self { Self { code: 0, @@ -74,23 +74,72 @@ impl Res { } } -impl ErrRes { - pub fn with_err(err: &str) -> Self { +impl ErrorResponseBuilder { + pub fn with_err(err: AppError) -> Self { + let (code, msg) = match &err { + AppError::AnyHow(e) => (500, e.to_string()), + AppError::ParseError(e) => (400, e.to_string()), + {{#if is_sqlx}} + AppError::SqlxError(e) => (500, e.to_string()), + {{/if}} + {{#if is_sea_orm}} + AppError::DbErr(e) => (500, e.to_string()), + {{/if}} + {{#if is_diesel}} + AppError::DieselErr(e) => (500, e.to_string()), + {{/if}} + {{#if is_rbatis}} + AppError::RbatisErr(e) => (500, e.to_string()), + {{/if}} + {{#if is_mongodb}} + AppError::MongoDbErr(e) => (500, e.to_string()), + AppError::MongoBsonAccessError(e) => (500, e.to_string()), + AppError::MongoBsonOidError(e) => (500, e.to_string()), + {{/if}} + {{#if need_db_conn}} + AppError::ValidationError(e) => (400, e.to_string()), + {{/if}} + }; Self { - code: 500, - msg: err.to_string(), + code, + msg, + source_error: err, } } } -impl Res { +impl ResponseBuilder { pub fn into_response(self, res: &mut Response) { res.render(Json(self)); } } -impl ErrRes { +impl ErrorResponseBuilder { pub fn into_response(self, res: &mut Response) { - res.stuff(StatusCode::INTERNAL_SERVER_ERROR, Json(self)); + let status_code = match self.source_error { + AppError::AnyHow(_) => StatusCode::INTERNAL_SERVER_ERROR, + AppError::ParseError(_) => StatusCode::BAD_REQUEST, + {{#if is_sqlx}} + AppError::SqlxError(_) => StatusCode::INTERNAL_SERVER_ERROR, + {{/if}} + {{#if is_sea_orm}} + AppError::DbErr(_) => StatusCode::INTERNAL_SERVER_ERROR, + {{/if}} + {{#if is_diesel}} + AppError::DieselErr(_) => StatusCode::INTERNAL_SERVER_ERROR, + {{/if}} + {{#if is_rbatis}} + AppError::RbatisErr(_) => StatusCode::INTERNAL_SERVER_ERROR, + {{/if}} + {{#if is_mongodb}} + AppError::MongoDbErr(_) => StatusCode::INTERNAL_SERVER_ERROR, + AppError::MongoBsonAccessError(_) => StatusCode::INTERNAL_SERVER_ERROR, + AppError::MongoBsonOidError(_) => StatusCode::INTERNAL_SERVER_ERROR, + {{/if}} + {{#if need_db_conn}} + AppError::ValidationError(_) => StatusCode::BAD_REQUEST, + {{/if}} + }; + res.stuff(status_code, Json(self)); } } @@ -99,7 +148,7 @@ pub type AppResult = Result; #[async_trait] impl Writer for AppError { async fn write(mut self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { - ErrRes::with_err(&self.to_string()).into_response(res) + ErrorResponseBuilder::with_err(self).into_response(res) } } diff --git a/src/template/src/routers/user.hbs b/src/template/src/routers/user.hbs index 7312399..dce23b7 100644 --- a/src/template/src/routers/user.hbs +++ b/src/template/src/routers/user.hbs @@ -1,6 +1,6 @@ {{#if is_web_site}} use crate::{ - app_response::{AppResponse, AppResult, ErrRes}, + app_response::{AppResponse, AppResult, ErrorResponseBuilder}, dtos::user::{ UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest, }, @@ -72,7 +72,7 @@ pub async fn post_login(req: JsonBody, res: &mut Response) { .build(); res.add_cookie(cookie); } - Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), + Err(e) => ErrorResponseBuilder::with_err(e).into_response(res), } } @@ -106,9 +106,11 @@ pub async fn get_users() -> AppResponse> { {{else}} use crate::{ - app_response::AppResult, - app_response::{ErrRes, Res}, - dtos::user::{UserAddRequest, UserLoginRequest, UserLoginResponse, UserUpdateRequest}, + app_response::ErrorResponseBuilder, + app_response::{AppResponse, AppResult}, + dtos::user::{ + UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest, + }, services::user, }; use salvo::{ @@ -130,47 +132,35 @@ pub async fn post_login(req: JsonBody, res: &mut Response) { .build(); res.add_cookie(cookie); } - Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), + Err(e) => ErrorResponseBuilder::with_err(e).into_response(res), } } -#[endpoint( tags("users"))] -pub async fn post_add_user(req: JsonBody, res: &mut Response) { - let result = user::add_user(req.0).await; - match result { - Ok(data) => Res::with_data(data).into_response(res), - Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), - } +#[endpoint(tags("users"))] +pub async fn post_add_user(new_user: JsonBody) -> AppResponse { + let result = user::add_user(new_user.0).await; + AppResponse(result) } #[endpoint( tags("users"), parameters( ("id", description = "user id"), ))] -pub async fn put_update_user(req: &mut Request, res: &mut Response) { - let req: UserUpdateRequest = req.extract().await.unwrap(); +pub async fn put_update_user(req: &mut Request) -> AppResult> { + let req: UserUpdateRequest = req.extract().await?; let result = user::update_user(req).await; - match result { - Ok(data) => Res::with_data(data).into_response(res), - Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), - } + Ok(AppResponse(result)) } -#[endpoint( tags("users"),)] -pub async fn delete_user(id: PathParam, res: &mut Response) { +#[endpoint(tags("users"))] +pub async fn delete_user(id: PathParam) -> AppResponse<()> { let result = user::delete_user(id.0).await; - match result { - Ok(_) => Res::with_data(()).into_response(res), - Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), - } + AppResponse(result) } -#[endpoint( tags("users"),)] -pub async fn get_users(res: &mut Response) { +#[endpoint(tags("users"))] +pub async fn get_users() -> AppResponse> { let result = user::users().await; - match result { - Ok(data) => Res::with_data(data).into_response(res), - Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), - } + AppResponse(result) } {{/if}}