1
1
use salvo::{
2
- async_trait,
3
- prelude::EndpointOutRegister,
4
- writing::Json,
5
- Depot, Request, Response, Writer, hyper::StatusCode,
2
+ async_trait, hyper::StatusCode, prelude::EndpointOutRegister, writing::Json, Depot, Request,
3
+ Response, Writer,
6
4
};
7
5
use serde::Serialize;
8
6
@@ -14,7 +12,7 @@ pub struct AppResponse<T>(pub AppResult<T>);
14
12
impl<T: Serialize + Default + Send> Writer for AppResponse<T > {
15
13
async fn write(self, req: & mut Request, depot: & mut Depot, res: & mut Response) {
16
14
match self.0 {
17
- Ok(data) => Res ::with_data(data).into_response(res),
15
+ Ok(data) => ResponseBuilder ::with_data(data).into_response(res),
18
16
Err(e) => e.write(req, depot, res).await,
19
17
}
20
18
}
@@ -44,19 +42,21 @@ impl<T> From<AppError> for AppResponse<T> {
44
42
}
45
43
46
44
#[derive(Debug, Serialize, Default)]
47
- pub struct Res <T > {
45
+ pub struct ResponseBuilder <T > {
48
46
pub code: i32,
49
47
pub data: T,
50
48
pub msg: String,
51
49
}
52
50
53
- #[derive(Debug, Serialize, Default )]
54
- pub struct ErrRes {
51
+ #[derive(Debug, Serialize)]
52
+ pub struct ErrorResponseBuilder {
55
53
pub code: i32,
56
54
pub msg: String,
55
+ #[serde(skip)]
56
+ pub source_error: AppError,
57
57
}
58
58
59
- impl<T: Serialize + Send + Default> Res <T > {
59
+ impl<T: Serialize + Send + Default> ResponseBuilder <T > {
60
60
pub fn with_data(data: T) -> Self {
61
61
Self {
62
62
code: 0,
@@ -74,23 +74,72 @@ impl<T: Serialize + Send + Default> Res<T> {
74
74
}
75
75
}
76
76
77
- impl ErrRes {
78
- pub fn with_err(err: & str) -> Self {
77
+ impl ErrorResponseBuilder {
78
+ pub fn with_err(err: AppError) -> Self {
79
+ let (code, msg) = match & err {
80
+ AppError::AnyHow(e) => (500, e.to_string()),
81
+ AppError::ParseError(e) => (400, e.to_string()),
82
+ {{ #if is_sqlx }}
83
+ AppError::SqlxError(e) => (500, e.to_string()),
84
+ {{ /if }}
85
+ {{ #if is_sea_orm }}
86
+ AppError::DbErr(e) => (500, e.to_string()),
87
+ {{ /if }}
88
+ {{ #if is_diesel }}
89
+ AppError::DieselErr(e) => (500, e.to_string()),
90
+ {{ /if }}
91
+ {{ #if is_rbatis }}
92
+ AppError::RbatisErr(e) => (500, e.to_string()),
93
+ {{ /if }}
94
+ {{ #if is_mongodb }}
95
+ AppError::MongoDbErr(e) => (500, e.to_string()),
96
+ AppError::MongoBsonAccessError(e) => (500, e.to_string()),
97
+ AppError::MongoBsonOidError(e) => (500, e.to_string()),
98
+ {{ /if }}
99
+ {{ #if need_db_conn }}
100
+ AppError::ValidationError(e) => (400, e.to_string()),
101
+ {{ /if }}
102
+ };
79
103
Self {
80
- code: 500,
81
- msg: err.to_string(),
104
+ code,
105
+ msg,
106
+ source_error: err,
82
107
}
83
108
}
84
109
}
85
- impl<T: Serialize + Send + Default> Res <T > {
110
+ impl<T: Serialize + Send + Default> ResponseBuilder <T > {
86
111
pub fn into_response(self, res: & mut Response) {
87
112
res.render(Json(self));
88
113
}
89
114
}
90
115
91
- impl ErrRes {
116
+ impl ErrorResponseBuilder {
92
117
pub fn into_response(self, res: & mut Response) {
93
- res.stuff(StatusCode::INTERNAL_SERVER_ERROR, Json(self));
118
+ let status_code = match self.source_error {
119
+ AppError::AnyHow(_) => StatusCode::INTERNAL_SERVER_ERROR,
120
+ AppError::ParseError(_) => StatusCode::BAD_REQUEST,
121
+ {{ #if is_sqlx }}
122
+ AppError::SqlxError(_) => StatusCode::INTERNAL_SERVER_ERROR,
123
+ {{ /if }}
124
+ {{ #if is_sea_orm }}
125
+ AppError::DbErr(_) => StatusCode::INTERNAL_SERVER_ERROR,
126
+ {{ /if }}
127
+ {{ #if is_diesel }}
128
+ AppError::DieselErr(_) => StatusCode::INTERNAL_SERVER_ERROR,
129
+ {{ /if }}
130
+ {{ #if is_rbatis }}
131
+ AppError::RbatisErr(_) => StatusCode::INTERNAL_SERVER_ERROR,
132
+ {{ /if }}
133
+ {{ #if is_mongodb }}
134
+ AppError::MongoDbErr(_) => StatusCode::INTERNAL_SERVER_ERROR,
135
+ AppError::MongoBsonAccessError(_) => StatusCode::INTERNAL_SERVER_ERROR,
136
+ AppError::MongoBsonOidError(_) => StatusCode::INTERNAL_SERVER_ERROR,
137
+ {{ /if }}
138
+ {{ #if need_db_conn }}
139
+ AppError::ValidationError(_) => StatusCode::BAD_REQUEST,
140
+ {{ /if }}
141
+ };
142
+ res.stuff(status_code, Json(self));
94
143
}
95
144
}
96
145
@@ -99,7 +148,7 @@ pub type AppResult<T> = Result<T, AppError>;
99
148
#[async_trait]
100
149
impl Writer for AppError {
101
150
async fn write(mut self, _req: & mut Request, _depot: & mut Depot, res: & mut Response) {
102
- ErrRes ::with_err(& self.to_string() ).into_response(res)
151
+ ErrorResponseBuilder ::with_err(self).into_response(res)
103
152
}
104
153
}
105
154
0 commit comments