@@ -109,7 +109,15 @@ pub async fn detect_schema(Json(json): Json<Value>) -> Result<impl Responder, St
109109 if !has_more_than_max_allowed_levels ( & json, 1 ) {
110110 //perform generic flattening, return error if failed to flatten
111111 let mut flattened_json = match generic_flattening ( & json) {
112- Ok ( flattened) => convert_to_array ( flattened) . unwrap ( ) ,
112+ Ok ( flattened) => match convert_to_array ( flattened) {
113+ Ok ( array) => array,
114+ Err ( e) => {
115+ return Err ( StreamError :: Custom {
116+ msg : format ! ( "Failed to convert to array: {}" , e) ,
117+ status : StatusCode :: BAD_REQUEST ,
118+ } )
119+ }
120+ } ,
113121 Err ( e) => {
114122 return Err ( StreamError :: Custom {
115123 msg : e. to_string ( ) ,
@@ -128,16 +136,26 @@ pub async fn detect_schema(Json(json): Json<Value>) -> Result<impl Responder, St
128136 value @ Value :: Object ( _) => vec ! [ value] ,
129137 _ => unreachable ! ( "flatten would have failed beforehand" ) ,
130138 } ;
131- let mut schema =
132- Arc :: new ( infer_json_schema_from_iterator ( flattened_json_arr. iter ( ) . map ( Ok ) ) . unwrap ( ) ) ;
139+ let mut schema = match infer_json_schema_from_iterator ( flattened_json_arr. iter ( ) . map ( Ok ) ) {
140+ Ok ( schema) => Arc :: new ( schema) ,
141+ Err ( e) => {
142+ return Err ( StreamError :: Custom {
143+ msg : format ! ( "Failed to infer schema: {}" , e) ,
144+ status : StatusCode :: BAD_REQUEST ,
145+ } )
146+ }
147+ } ;
133148 for flattened_json in flattened_json_arr {
134149 schema = override_data_type ( schema, flattened_json, SchemaVersion :: V1 ) ;
135150 }
136151 Ok ( ( web:: Json ( schema) , StatusCode :: OK ) )
137152 } else {
138153 // error out if the JSON is heavily nested
139154 Err ( StreamError :: Custom {
140- msg : "heavily nested, cannot flatten this JSON" . to_string ( ) ,
155+ msg : format ! (
156+ "JSON is too deeply nested (exceeds level {}), cannot flatten" ,
157+ PARSEABLE . options. event_flatten_level
158+ ) ,
141159 status : StatusCode :: BAD_REQUEST ,
142160 } )
143161 }
0 commit comments