Skip to content

Commit e0cc7c4

Browse files
author
Michael Brewer
committed
feat(idempotency): Fix when delete_record raise a KeyError
Fix a KeyError when we an error happens and local_cache is True and the cache is empty
1 parent fe53a2e commit e0cc7c4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Diff for: aws_lambda_powertools/utilities/idempotency/persistence/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def _retrieve_from_cache(self, idempotency_key: str):
272272
def _delete_from_cache(self, idempotency_key: str):
273273
if not self.use_local_cache:
274274
return
275-
del self._cache[idempotency_key]
275+
if idempotency_key in self._cache:
276+
del self._cache[idempotency_key]
276277

277278
def save_success(self, event: Dict[str, Any], result: dict) -> None:
278279
"""

Diff for: tests/functional/idempotency/test_idempotency.py

+11
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,14 @@ def test_user_local_disabled(persistence_store):
627627
# THEN raise AttributeError
628628
# AND don't have a _cache attribute
629629
assert not hasattr("persistence_store", "_cache")
630+
631+
632+
@pytest.mark.parametrize("persistence_store", [{"use_local_cache": True}], indirect=True)
633+
def test_delete_from_cache_when_empty(persistence_store):
634+
# GIVEN use_local_cache is True AND the local cache is empty
635+
try:
636+
# WHEN we _delete_from_cache
637+
persistence_store._delete_from_cache("key_does_not_exist")
638+
except KeyError:
639+
# THEN we should not get a KeyError
640+
pytest.fail("KeyError should not happen")

0 commit comments

Comments
 (0)