@@ -2,8 +2,6 @@ use super::SwitchError;
2
2
use axum:: response:: { IntoResponse , Response } ;
3
3
use hyper:: StatusCode ;
4
4
5
- use error_stack:: ResultExt ;
6
-
7
5
pub type ApiResponseResult < T > = Result < T , ApiErrorContainer > ;
8
6
9
7
pub struct ApiErrorContainer {
@@ -44,13 +42,15 @@ struct ApiErrorResponse<'a> {
44
42
45
43
#[ derive( Debug , thiserror:: Error ) ]
46
44
pub enum ApplicationErrorResponse {
47
- #[ error( "Something Went Wrong " ) ]
48
- InternalServerError ,
45
+ #[ error( "Internal Server Error Occurred - {0} " ) ]
46
+ InternalServerError ( & ' static str ) ,
49
47
#[ error( "The resource was not found in the {0}" ) ]
50
48
NotFound ( & ' static str ) ,
51
49
#[ error( "Invalid request provided {0}" ) ]
52
50
ParsingFailed ( String ) ,
53
- #[ error( "Unique violation occured. Please try to create the data with another key/identifier" ) ]
51
+ #[ error(
52
+ "Unique violation occurred. Please try to create the data with another key/identifier"
53
+ ) ]
54
54
UniqueViolation ,
55
55
}
56
56
@@ -69,7 +69,27 @@ impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, Pars
69
69
70
70
impl < T > SwitchError < T , ApplicationErrorResponse > for super :: CustomResult < T , super :: CryptoError > {
71
71
fn switch ( self ) -> super :: CustomResult < T , ApplicationErrorResponse > {
72
- self . change_context ( ApplicationErrorResponse :: InternalServerError )
72
+ self . map_err ( |err| {
73
+ let new_err = match err. current_context ( ) {
74
+ super :: CryptoError :: EncryptionFailed ( _) => {
75
+ ApplicationErrorResponse :: InternalServerError ( "Encryption failed" )
76
+ }
77
+ super :: CryptoError :: DecryptionFailed ( _) => {
78
+ ApplicationErrorResponse :: InternalServerError ( "Decryption failed" )
79
+ }
80
+ super :: CryptoError :: KeyGeneration => {
81
+ ApplicationErrorResponse :: InternalServerError ( "Key generation failed" )
82
+ }
83
+ super :: CryptoError :: InvalidKey => {
84
+ ApplicationErrorResponse :: InternalServerError ( "Invalid key detected" )
85
+ }
86
+ super :: CryptoError :: KeyGetFailed => {
87
+ ApplicationErrorResponse :: InternalServerError ( "Failed to get the key" )
88
+ }
89
+ _ => ApplicationErrorResponse :: InternalServerError ( "Unexpected error occurred" ) ,
90
+ } ;
91
+ err. change_context ( new_err)
92
+ } )
73
93
}
74
94
}
75
95
@@ -81,7 +101,9 @@ impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, supe
81
101
super :: DatabaseError :: ConnectionError ( _)
82
102
| super :: DatabaseError :: NotNullViolation
83
103
| super :: DatabaseError :: InvalidValue
84
- | super :: DatabaseError :: Others => ApplicationErrorResponse :: InternalServerError ,
104
+ | super :: DatabaseError :: Others => {
105
+ ApplicationErrorResponse :: InternalServerError ( "Database error occurred" )
106
+ }
85
107
super :: DatabaseError :: UniqueViolation => ApplicationErrorResponse :: UniqueViolation ,
86
108
} ;
87
109
err. change_context ( new_err)
@@ -98,7 +120,7 @@ impl<T> ToContainerError<T> for super::CustomResult<T, ApplicationErrorResponse>
98
120
impl IntoResponse for ApiErrorContainer {
99
121
fn into_response ( self ) -> Response {
100
122
match self . error . current_context ( ) {
101
- err @ ApplicationErrorResponse :: InternalServerError => (
123
+ err @ ApplicationErrorResponse :: InternalServerError ( _ ) => (
102
124
StatusCode :: INTERNAL_SERVER_ERROR ,
103
125
axum:: Json ( ApiErrorResponse {
104
126
error_message : err. to_string ( ) ,
0 commit comments