11use axum:: Json ;
22use axum:: http:: StatusCode ;
33use axum:: response:: { IntoResponse , Response } ;
4- use sqlx :: Error ;
4+ use sea_orm :: DbErr ;
55use reqwest:: Error as HttpError ;
66use redis:: RedisError ;
77use log:: error;
@@ -24,7 +24,7 @@ pub(crate) struct ErrResponse {
2424#[ derive( Debug ) ]
2525pub enum AppError {
2626 DbError {
27- source : Error ,
27+ source : DbErr ,
2828 backtrace : Backtrace ,
2929 } ,
3030 RedisError {
@@ -48,8 +48,8 @@ pub enum AppError {
4848 LoginFailure ,
4949}
5050
51- impl From < Error > for AppError {
52- fn from ( value : Error ) -> Self {
51+ impl From < DbErr > for AppError {
52+ fn from ( value : DbErr ) -> Self {
5353 error ! ( "数据库错误:{:?}" , value) ;
5454 Self :: DbError { source : value, backtrace : Backtrace :: capture ( ) }
5555 }
@@ -126,19 +126,19 @@ impl IntoResponse for AppError {
126126 eprintln ! ( "Error: {}" , self ) ;
127127 let resp = match self {
128128 Self :: DbError { source, backtrace} =>{
129- if let Error :: RowNotFound = source {
130- //error!("查询记录不存在:{:?} {:?}", source, backtrace);
131- (
132- StatusCode :: NOT_FOUND ,
133- Json ( json ! ( ErrResponse { info: format!( "{source:?}" ) } ) )
134- )
135- } else {
136- //error!("数据库错误:{:?} {:?}", source, backtrace);
137- (
138- StatusCode :: INTERNAL_SERVER_ERROR ,
139- Json ( json ! ( ErrResponse { info: format!( "{source:?}" ) } ) )
140- )
141-
129+ match source {
130+ DbErr :: RecordNotFound ( _ ) => {
131+ (
132+ StatusCode :: NOT_FOUND ,
133+ Json ( json ! ( ErrResponse { info: format!( "{source:?}" ) } ) )
134+ )
135+ }
136+ _=> {
137+ (
138+ StatusCode :: INTERNAL_SERVER_ERROR ,
139+ Json ( json ! ( ErrResponse { info: format!( "{source:?}" ) } ) )
140+ )
141+ }
142142 }
143143 } ,
144144 Self :: LogicError ( msg) =>{
0 commit comments