Skip to content

Commit 7351c78

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 7351c78

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tests/bedrock_agentcore/runtime/test_app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,50 @@ 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, (
517+
f"Handler JSONDecodeError should return 500, got {response.status_code}"
518+
)
519+
assert "Invalid JSON" not in response.json().get("error", ""), (
520+
"Handler JSONDecodeError should not say 'Invalid JSON' (that implies bad request)"
521+
)
522+
523+
def test_invalid_request_json_returns_400(self):
524+
"""Test that malformed JSON in the request body returns 400 with 'Invalid JSON'."""
525+
app = BedrockAgentCoreApp()
526+
527+
@app.entrypoint
528+
def handler(payload):
529+
return payload
530+
531+
client = TestClient(app, raise_server_exceptions=False)
532+
response = client.post(
533+
"/invocations",
534+
content=b"not valid json",
535+
headers={"content-type": "application/json"},
536+
)
537+
538+
assert response.status_code == 400
539+
assert "Invalid JSON" in response.json()["error"]
540+
497541
def test_return_response_passthrough(self):
498542
"""Test returning a Response object passes it through without wrapping."""
499543
from starlette.responses import JSONResponse

0 commit comments

Comments
 (0)