@@ -540,6 +540,16 @@ async def _handle_invocation(self, request):
540540
541541 try :
542542 payload = await request .json ()
543+ except json .JSONDecodeError as e :
544+ duration = time .time () - start_time
545+ self .logger .warning ("Invalid JSON in request (%.3fs): %s" , duration , e )
546+ return JSONResponse ({"error" : "Invalid JSON" , "details" : str (e )}, status_code = 400 )
547+ except UnicodeDecodeError as e :
548+ duration = time .time () - start_time
549+ self .logger .warning ("Invalid encoding in request (%.3fs): %s" , duration , e )
550+ return JSONResponse ({"error" : "Invalid encoding" , "details" : str (e )}, status_code = 400 )
551+
552+ try :
543553 self .logger .debug ("Processing invocation request" )
544554
545555 if self .debug :
@@ -584,15 +594,6 @@ async def _handle_invocation(self, request):
584594 # Use safe serialization for consistency with streaming paths
585595 safe_json_string = self ._safe_serialize_to_json_string (result )
586596 return Response (safe_json_string , media_type = "application/json" )
587-
588- except json .JSONDecodeError as e :
589- duration = time .time () - start_time
590- self .logger .warning ("Invalid JSON in request (%.3fs): %s" , duration , e )
591- return JSONResponse ({"error" : "Invalid JSON" , "details" : str (e )}, status_code = 400 )
592- except UnicodeDecodeError as e :
593- duration = time .time () - start_time
594- self .logger .warning ("Invalid encoding in request (%.3fs): %s" , duration , e )
595- return JSONResponse ({"error" : "Invalid encoding" , "details" : str (e )}, status_code = 400 )
596597 except HTTPException as e :
597598 duration = time .time () - start_time
598599 # Use error level for 5xx to match the generic Exception handler's severity,
0 commit comments