@@ -136,21 +136,32 @@ async fn shutdown_signal() {
136136}
137137
138138/// High-level errors to return to clients.
139- /// Validation errors are forwarded from downstream clients.
140139#[ derive( Debug , thiserror:: Error ) ]
141140pub enum Error {
142141 #[ error( "{0}" ) ]
143- ValidationError ( String ) ,
142+ Validation ( String ) ,
143+ #[ error( "{0}" ) ]
144+ NotFound ( String ) ,
144145 #[ error( "unexpected error occured while processing request" ) ]
145- UnexpectedError ,
146+ Unexpected ,
146147}
147148
148149impl From < orchestrator:: Error > for Error {
149150 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 ,
154165 }
155166 }
156167}
@@ -159,12 +170,13 @@ impl IntoResponse for Error {
159170 fn into_response ( self ) -> Response {
160171 use Error :: * ;
161172 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 ( ) ) ,
164176 } ;
165177 let error = serde_json:: json!( {
166178 "code" : code. as_u16( ) ,
167- "message " : message,
179+ "details " : message,
168180 } ) ;
169181 ( code, Json ( error) ) . into_response ( )
170182 }
0 commit comments