Skip to content

Commit

Permalink
update better erroe handle
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Dec 7, 2023
1 parent 1d486e0 commit 7cb2f58
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 48 deletions.
83 changes: 66 additions & 17 deletions src/template/src/app_response.hbs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,7 +12,7 @@ pub struct AppResponse<T>(pub AppResult<T>);
impl<T: Serialize + Default + Send> Writer for AppResponse<T> {
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,
}
}
Expand Down Expand Up @@ -44,19 +42,21 @@ impl<T> From<AppError> for AppResponse<T> {
}

#[derive(Debug, Serialize, Default)]
pub struct Res<T> {
pub struct ResponseBuilder<T> {
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<T: Serialize + Send + Default> Res<T> {
impl<T: Serialize + Send + Default> ResponseBuilder<T> {
pub fn with_data(data: T) -> Self {
Self {
code: 0,
Expand All @@ -74,23 +74,72 @@ impl<T: Serialize + Send + Default> Res<T> {
}
}

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<T: Serialize + Send + Default> Res<T> {
impl<T: Serialize + Send + Default> ResponseBuilder<T> {
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));
}
}

Expand All @@ -99,7 +148,7 @@ pub type AppResult<T> = Result<T, AppError>;
#[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)
}
}

Expand Down
52 changes: 21 additions & 31 deletions src/template/src/routers/user.hbs
Original file line number Diff line number Diff line change
@@ -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,
},
Expand Down Expand Up @@ -72,7 +72,7 @@ pub async fn post_login(req: JsonBody<UserLoginRequest>, 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),
}
}

Expand Down Expand Up @@ -106,9 +106,11 @@ pub async fn get_users() -> AppResponse<Vec<UserResponse>> {

{{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::{
Expand All @@ -130,47 +132,35 @@ pub async fn post_login(req: JsonBody<UserLoginRequest>, 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<UserAddRequest>, 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<UserAddRequest>) -> AppResponse<UserResponse> {
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<AppResponse<UserResponse>> {
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<String>, res: &mut Response) {
#[endpoint(tags("users"))]
pub async fn delete_user(id: PathParam<String>) -> 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<Vec<UserResponse>> {
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}}

0 comments on commit 7cb2f58

Please sign in to comment.