Skip to content

fix(idempotent): Correctly raise IdempotencyKeyError #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws_lambda_powertools/utilities/idempotency/idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
IdempotencyInconsistentStateError,
IdempotencyItemAlreadyExistsError,
IdempotencyItemNotFoundError,
IdempotencyKeyError,
IdempotencyPersistenceLayerError,
IdempotencyValidationError,
)
Expand Down Expand Up @@ -132,6 +133,8 @@ def handle(self) -> Any:
# We call save_inprogress first as an optimization for the most common case where no idempotent record
# already exists. If it succeeds, there's no need to call get_record.
self.persistence_store.save_inprogress(event=self.event, context=self.context)
except IdempotencyKeyError:
raise
except IdempotencyItemAlreadyExistsError:
# Now we know the item already exists, we can retrieve it
record = self._get_idempotency_record()
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,20 @@ def lambda_handler(event, context):
stubber.assert_no_pending_responses()
stubber.deactivate()
assert "Failed to save in progress record to idempotency store" == e.value.args[0]


def test_handler_raise_idempotency_key_error(persistence_store: DynamoDBPersistenceLayer, lambda_context):
# GIVEN raise_on_no_idempotency_key is True
idempotency_config = IdempotencyConfig(event_key_jmespath="idemKey", raise_on_no_idempotency_key=True)

# WHEN handling the idempotent call
# AND save_inprogress raises a IdempotencyKeyError
@idempotent(persistence_store=persistence_store, config=idempotency_config)
def handler(event, context):
raise ValueError("Should not be raised")

# THEN idempotent should re-raise the IdempotencyKeyError
with pytest.raises(IdempotencyKeyError) as e:
handler({}, lambda_context)

assert "No data found to create a hashed idempotency_key" == e.value.args[0]