@@ -136,21 +136,32 @@ async fn shutdown_signal() {
136
136
}
137
137
138
138
/// High-level errors to return to clients.
139
- /// Validation errors are forwarded from downstream clients.
140
139
#[ derive( Debug , thiserror:: Error ) ]
141
140
pub enum Error {
142
141
#[ error( "{0}" ) ]
143
- ValidationError ( String ) ,
142
+ Validation ( String ) ,
143
+ #[ error( "{0}" ) ]
144
+ NotFound ( String ) ,
144
145
#[ error( "unexpected error occured while processing request" ) ]
145
- UnexpectedError ,
146
+ Unexpected ,
146
147
}
147
148
148
149
impl From < orchestrator:: Error > for Error {
149
150
fn from ( error : orchestrator:: Error ) -> Self {
150
- if error. is_validation_error ( ) {
151
- Self :: ValidationError ( error. to_string ( ) )
152
- } else {
153
- Self :: UnexpectedError
151
+ use orchestrator:: Error :: * ;
152
+ match error {
153
+ DetectorNotFound { .. } => Self :: NotFound ( error. to_string ( ) ) ,
154
+ DetectorRequestFailed { error, .. }
155
+ | ChunkerRequestFailed { error, .. }
156
+ | GenerateRequestFailed { error, .. }
157
+ | TokenizeRequestFailed { error, .. } => match error. status_code ( ) {
158
+ StatusCode :: BAD_REQUEST | StatusCode :: UNPROCESSABLE_ENTITY => {
159
+ Self :: Validation ( error. to_string ( ) )
160
+ }
161
+ StatusCode :: NOT_FOUND => Self :: NotFound ( error. to_string ( ) ) ,
162
+ _ => Self :: Unexpected ,
163
+ } ,
164
+ _ => Self :: Unexpected ,
154
165
}
155
166
}
156
167
}
@@ -159,12 +170,13 @@ impl IntoResponse for Error {
159
170
fn into_response ( self ) -> Response {
160
171
use Error :: * ;
161
172
let ( code, message) = match self {
162
- ValidationError ( _) => ( StatusCode :: UNPROCESSABLE_ENTITY , self . to_string ( ) ) ,
163
- UnexpectedError => ( StatusCode :: INTERNAL_SERVER_ERROR , self . to_string ( ) ) ,
173
+ Validation ( _) => ( StatusCode :: UNPROCESSABLE_ENTITY , self . to_string ( ) ) ,
174
+ NotFound ( _) => ( StatusCode :: NOT_FOUND , self . to_string ( ) ) ,
175
+ Unexpected => ( StatusCode :: INTERNAL_SERVER_ERROR , self . to_string ( ) ) ,
164
176
} ;
165
177
let error = serde_json:: json!( {
166
178
"code" : code. as_u16( ) ,
167
- "message " : message,
179
+ "details " : message,
168
180
} ) ;
169
181
( code, Json ( error) ) . into_response ( )
170
182
}
0 commit comments