From 88a0a935dc6a65ec4a854a8391eb8844e0188d17 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Sat, 6 Mar 2021 09:00:06 -0800 Subject: [PATCH 1/2] fix(idempotency): Handle save_inprogress errors as a IdempotencyPersistenceLayerError --- .../utilities/idempotency/idempotency.py | 2 ++ .../idempotency/test_idempotency.py | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/aws_lambda_powertools/utilities/idempotency/idempotency.py b/aws_lambda_powertools/utilities/idempotency/idempotency.py index 06d77a9fd72..750ed360f0b 100644 --- a/aws_lambda_powertools/utilities/idempotency/idempotency.py +++ b/aws_lambda_powertools/utilities/idempotency/idempotency.py @@ -135,6 +135,8 @@ def handle(self) -> Any: # Now we know the item already exists, we can retrieve it record = self._get_idempotency_record() return self._handle_for_status(record) + except Exception as exc: + raise IdempotencyPersistenceLayerError("Failed to save in progress record to idempotency store") from exc return self._call_lambda_handler() diff --git a/tests/functional/idempotency/test_idempotency.py b/tests/functional/idempotency/test_idempotency.py index 6def9b4868a..a5f9f793137 100644 --- a/tests/functional/idempotency/test_idempotency.py +++ b/tests/functional/idempotency/test_idempotency.py @@ -775,3 +775,27 @@ def test_custom_jmespath_function_overrides_builtin_functions( # WHEN calling _get_hashed_idempotency_key # THEN raise unknown function persistence_store._get_hashed_idempotency_key({}) + + +@pytest.mark.parametrize("config_with_validation", [{"use_local_cache": False}], indirect=True) +def test_idempotent_lambda_save_inprogress_error( + config_with_validation: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer +): + """ + Test idempotent decorator where event with matching event key has already been successfully processed + """ + + stubber = stub.Stubber(persistence_store.table.meta.client) + stubber.add_client_error("put_item", "ClientError") + stubber.activate() + + @idempotent(config=config_with_validation, persistence_store=persistence_store) + def lambda_handler(event, context): + return {} + + with pytest.raises(IdempotencyPersistenceLayerError) as e: + lambda_handler({}, {}) + + stubber.assert_no_pending_responses() + stubber.deactivate() + assert "Failed to save in progress record to idempotency store" == e.value.args[0] From b98b5ecf5686342865b3497616c28331643f4b17 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Sat, 6 Mar 2021 09:08:48 -0800 Subject: [PATCH 2/2] chore: Correct docs for test --- .../functional/idempotency/test_idempotency.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/functional/idempotency/test_idempotency.py b/tests/functional/idempotency/test_idempotency.py index a5f9f793137..2f541587227 100644 --- a/tests/functional/idempotency/test_idempotency.py +++ b/tests/functional/idempotency/test_idempotency.py @@ -777,25 +777,23 @@ def test_custom_jmespath_function_overrides_builtin_functions( persistence_store._get_hashed_idempotency_key({}) -@pytest.mark.parametrize("config_with_validation", [{"use_local_cache": False}], indirect=True) -def test_idempotent_lambda_save_inprogress_error( - config_with_validation: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer -): - """ - Test idempotent decorator where event with matching event key has already been successfully processed - """ - +def test_idempotent_lambda_save_inprogress_error(persistence_store: DynamoDBPersistenceLayer): + # GIVEN a miss configured persistence layer + # like no table was created for the idempotency persistence layer stubber = stub.Stubber(persistence_store.table.meta.client) - stubber.add_client_error("put_item", "ClientError") + stubber.add_client_error("put_item", "ResourceNotFoundException") stubber.activate() - @idempotent(config=config_with_validation, persistence_store=persistence_store) + @idempotent(persistence_store=persistence_store) def lambda_handler(event, context): return {} + # WHEN handling the idempotent call + # AND save_inprogress raises a ClientError with pytest.raises(IdempotencyPersistenceLayerError) as e: lambda_handler({}, {}) + # THEN idempotent should raise an IdempotencyPersistenceLayerError stubber.assert_no_pending_responses() stubber.deactivate() assert "Failed to save in progress record to idempotency store" == e.value.args[0]