Skip to content

Commit a9a220e

Browse files
committed
fix: add crash test
1 parent 73c0295 commit a9a220e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def __init__(
241241
self.headers: Dict[str, Union[str, List[str]]] = headers if headers else {}
242242
self.cookies = cookies or []
243243
self.compress = compress
244+
self.content_type = content_type
244245
if content_type:
245246
self.headers.setdefault("Content-Type", content_type)
246247

aws_lambda_powertools/event_handler/bedrock_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def build(self, event: BedrockAgentEvent, *args) -> Dict[str, Any]:
3030
"httpMethod": event.http_method,
3131
"httpStatusCode": self.response.status_code,
3232
"responseBody": {
33-
"application/json": {
33+
self.response.content_type: {
3434
"body": self.response.body,
3535
},
3636
},

tests/functional/event_handler/test_bedrock_agent.py

+31
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,34 @@ def claims():
7777
assert result["response"]["actionGroup"] == "ClaimManagementActionGroup"
7878
assert result["response"]["httpMethod"] == "GET"
7979
assert result["response"]["httpStatusCode"] == 404
80+
81+
82+
def test_bedrock_agent_event_with_exception():
83+
# GIVEN a Bedrock Agent event
84+
app = BedrockAgentResolver()
85+
86+
@app.exception_handler(RuntimeError)
87+
def handle_runtime_error(ex: RuntimeError):
88+
return Response(
89+
status_code=500,
90+
content_type=content_types.TEXT_PLAIN,
91+
body="Something went wrong",
92+
)
93+
94+
@app.get("/claims")
95+
def claims():
96+
raise RuntimeError()
97+
98+
# WHEN calling the event handler
99+
result = app(load_event("bedrockAgentEvent.json"), {})
100+
101+
# THEN process the exception correctly
102+
# AND return 500 because of the internal server error
103+
assert result["messageVersion"] == "1.0"
104+
assert result["response"]["apiPath"] == "/claims"
105+
assert result["response"]["actionGroup"] == "ClaimManagementActionGroup"
106+
assert result["response"]["httpMethod"] == "GET"
107+
assert result["response"]["httpStatusCode"] == 500
108+
109+
body = result["response"]["responseBody"]["text/plain"]["body"]
110+
assert body == "Something went wrong"

0 commit comments

Comments
 (0)