1- use axum:: {
2- http:: StatusCode ,
3- response:: { IntoResponse , Response } ,
4- Json ,
5- } ;
1+ use axum:: http:: StatusCode ;
62use sdk:: dto:: response:: BodyResponseError ;
73
8- pub type ApiResult < T = ( ) > = std:: result:: Result < T , ApiError > ;
4+ pub mod response;
5+ pub mod result;
96
107#[ derive( Debug , thiserror:: Error ) ]
118pub enum ApiError {
@@ -53,6 +50,8 @@ pub enum ApiError {
5350 MultipartError ( #[ from] axum:: extract:: multipart:: MultipartError ) ,
5451 #[ error( transparent) ]
5552 UrlError ( #[ from] url:: ParseError ) ,
53+ #[ error( transparent) ]
54+ ImageError ( #[ from] image:: ImageError ) ,
5655 #[ error( "lock error: {0}" ) ]
5756 LockError ( String ) ,
5857 #[ error( "duration out of range error: {0}" ) ]
@@ -155,6 +154,11 @@ impl ApiError {
155154 err. to_string ( ) ,
156155 StatusCode :: INTERNAL_SERVER_ERROR ,
157156 ) ,
157+ ImageError ( err) => (
158+ "IMAGE_ERROR" ,
159+ err. to_string ( ) ,
160+ StatusCode :: INTERNAL_SERVER_ERROR ,
161+ ) ,
158162 UnknownError ( err) => (
159163 "UNKNOWN_ERROR" ,
160164 err. to_string ( ) ,
@@ -178,25 +182,8 @@ impl ApiError {
178182 }
179183}
180184
181- impl IntoResponse for ApiError {
182- fn into_response ( self ) -> Response {
183- let ( body, status_code) = self . response ( ) ;
184- ( status_code, Json ( body) ) . into_response ( )
185- }
186- }
187-
188185pub fn invalid_input_error ( field : & ' static str , message : & ' static str ) -> ApiError {
189186 let mut report = garde:: Report :: new ( ) ;
190187 report. append ( garde:: Path :: new ( field) , garde:: Error :: new ( message) ) ;
191188 ApiError :: InvalidInputError ( report)
192189}
193-
194- pub trait ToApiResult < T > {
195- fn to_result ( self ) -> ApiResult < T > ;
196- }
197-
198- impl < T > ToApiResult < T > for Option < T > {
199- fn to_result ( self ) -> ApiResult < T > {
200- self . ok_or_else ( || ApiError :: NotFoundError ( format ! ( "{} not found" , std:: any:: type_name:: <T >( ) ) ) )
201- }
202- }
0 commit comments