Skip to content

Commit 4153672

Browse files
authored
adding json decoding runtime error handling (#561)
1 parent db80713 commit 4153672

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/rpdk/core/contract/resource_client.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,17 @@ def _call(self, payload):
432432
result = self._client.invoke(
433433
FunctionName=self._function_name, Payload=payload.encode("utf-8")
434434
)
435-
payload = json.load(result["Payload"])
436-
LOG.debug("Received response\n%s", payload)
437-
return payload
435+
436+
try:
437+
payload = json.load(result["Payload"])
438+
except json.decoder.JSONDecodeError as json_error:
439+
LOG.debug("Received invalid response\n%s", result["Payload"])
440+
raise ValueError(
441+
"Handler Output is not a valid JSON document"
442+
) from json_error
443+
else:
444+
LOG.debug("Received response\n%s", payload)
445+
return payload
438446

439447
def call_and_assert(
440448
self, action, assert_status, current_model, previous_model=None, **kwargs

tests/contract/test_resource_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,16 @@ def test_call_and_assert_success(resource_client):
614614
assert error_code is None
615615

616616

617+
def test_call_and_assert_failed_invalid_payload(resource_client):
618+
mock_client = resource_client._client
619+
mock_client.invoke.return_value = {"Payload": StringIO("invalid json document")}
620+
621+
with pytest.raises(ValueError):
622+
status, response, error_code = resource_client.call_and_assert(
623+
Action.CREATE, OperationStatus.SUCCESS, {}, None
624+
)
625+
626+
617627
def test_call_and_assert_failed(resource_client):
618628
mock_client = resource_client._client
619629
mock_client.invoke.return_value = {

0 commit comments

Comments
 (0)