Skip to content

Commit b690b15

Browse files
committed
fix(runtime): distinguish request parse errors from handler JSON errors
The broad try/except around _handle_invocation caught json.JSONDecodeError from both request.json() (inbound payload) and the user's handler logic, reporting both as "Invalid JSON in request" with HTTP 400. This misled engineers into debugging caller payloads when the root cause was downstream application/model-output parsing. Narrow the JSONDecodeError and UnicodeDecodeError catches to only cover request body parsing. Handler-raised exceptions now fall through to the generic Exception handler (HTTP 500) with an accurate error message. Constraint: Runtime contract returns 4xx for request-level errors only Rejected: Catch handler JSONDecodeError separately with 422 | no precedent in runtime contract Confidence: high Scope-risk: narrow Not-tested: Streaming handlers that raise JSONDecodeError mid-stream
1 parent eddc27c commit b690b15

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

  • src/bedrock_agentcore/runtime

src/bedrock_agentcore/runtime/app.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,16 @@ async def _handle_invocation(self, request):
537537

538538
try:
539539
payload = await request.json()
540+
except json.JSONDecodeError as e:
541+
duration = time.time() - start_time
542+
self.logger.warning("Invalid JSON in request (%.3fs): %s", duration, e)
543+
return JSONResponse({"error": "Invalid JSON", "details": str(e)}, status_code=400)
544+
except UnicodeDecodeError as e:
545+
duration = time.time() - start_time
546+
self.logger.warning("Invalid encoding in request (%.3fs): %s", duration, e)
547+
return JSONResponse({"error": "Invalid encoding", "details": str(e)}, status_code=400)
548+
549+
try:
540550
self.logger.debug("Processing invocation request")
541551

542552
if self.debug:
@@ -581,15 +591,6 @@ async def _handle_invocation(self, request):
581591
# Use safe serialization for consistency with streaming paths
582592
safe_json_string = self._safe_serialize_to_json_string(result)
583593
return Response(safe_json_string, media_type="application/json")
584-
585-
except json.JSONDecodeError as e:
586-
duration = time.time() - start_time
587-
self.logger.warning("Invalid JSON in request (%.3fs): %s", duration, e)
588-
return JSONResponse({"error": "Invalid JSON", "details": str(e)}, status_code=400)
589-
except UnicodeDecodeError as e:
590-
duration = time.time() - start_time
591-
self.logger.warning("Invalid encoding in request (%.3fs): %s", duration, e)
592-
return JSONResponse({"error": "Invalid encoding", "details": str(e)}, status_code=400)
593594
except HTTPException as e:
594595
duration = time.time() - start_time
595596
# Use error level for 5xx to match the generic Exception handler's severity,

0 commit comments

Comments
 (0)