Skip to content

Commit 040b523

Browse files
committed
test: add regression test for handler JSONDecodeError misclassification
Ensures that a JSONDecodeError raised inside the user's handler returns HTTP 500 (application error), not HTTP 400 "Invalid JSON" which misleadingly implies the inbound request payload was malformed.
1 parent b690b15 commit 040b523

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tests/bedrock_agentcore/runtime/test_app.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,48 @@ def handler(payload):
494494
assert response.status_code == 400
495495
assert "Invalid encoding" in response.json()["error"]
496496

497+
def test_handler_json_decode_error_returns_500_not_400(self):
498+
"""Test that JSONDecodeError raised inside the handler returns 500, not 400.
499+
500+
Regression test: previously, a broad except caught JSONDecodeError from
501+
both request parsing and handler logic, returning a misleading 400
502+
"Invalid JSON in request" for application-level parse failures.
503+
"""
504+
app = BedrockAgentCoreApp()
505+
506+
@app.entrypoint
507+
def handler(payload):
508+
import json
509+
510+
# Simulate parsing model output that isn't valid JSON
511+
json.loads("not valid json from model")
512+
513+
client = TestClient(app, raise_server_exceptions=False)
514+
response = client.post("/invocations", json={"input": "test"})
515+
516+
assert response.status_code == 500, f"Handler JSONDecodeError should return 500, got {response.status_code}"
517+
assert "Invalid JSON" not in response.json().get("error", ""), (
518+
"Handler JSONDecodeError should not say 'Invalid JSON' (that implies bad request)"
519+
)
520+
521+
def test_invalid_request_json_returns_400(self):
522+
"""Test that malformed JSON in the request body returns 400 with 'Invalid JSON'."""
523+
app = BedrockAgentCoreApp()
524+
525+
@app.entrypoint
526+
def handler(payload):
527+
return payload
528+
529+
client = TestClient(app, raise_server_exceptions=False)
530+
response = client.post(
531+
"/invocations",
532+
content=b"not valid json",
533+
headers={"content-type": "application/json"},
534+
)
535+
536+
assert response.status_code == 400
537+
assert "Invalid JSON" in response.json()["error"]
538+
497539
def test_return_response_passthrough(self):
498540
"""Test returning a Response object passes it through without wrapping."""
499541
from starlette.responses import JSONResponse

0 commit comments

Comments
 (0)