Skip to content

Commit 01b8fa5

Browse files
author
Michal Ploski
committed
Log nested exception for idempotency feature
1 parent b49aa6c commit 01b8fa5

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

aws_lambda_powertools/utilities/idempotency/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ def _process_idempotency(self):
113113
record = self._get_idempotency_record()
114114
return self._handle_for_status(record)
115115
except Exception as exc:
116-
raise IdempotencyPersistenceLayerError("Failed to save in progress record to idempotency store") from exc
116+
raise IdempotencyPersistenceLayerError(
117+
"Failed to save in progress record to idempotency store", exc
118+
) from exc
117119

118120
return self._get_function_response()
119121

aws_lambda_powertools/utilities/idempotency/exceptions.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,68 @@
33
"""
44

55

6-
class IdempotencyItemAlreadyExistsError(Exception):
6+
class BaseError(Exception):
7+
"""
8+
Base error class that overwrites the way exception and extra information is printed.
9+
See https://github.com/awslabs/aws-lambda-powertools-python/issues/1772
10+
"""
11+
12+
def format_msg(self, *args: str) -> str:
13+
full_msg = str(args[0])
14+
if args[1:]:
15+
full_msg += f': ({"".join(str(arg) for arg in args[1:])})'
16+
return full_msg
17+
18+
def __init__(self, *args: str):
19+
if args:
20+
super().__init__(self.format_msg(*args))
21+
else:
22+
super().__init__()
23+
24+
25+
class IdempotencyItemAlreadyExistsError(BaseError):
726
"""
827
Item attempting to be inserted into persistence store already exists and is not expired
928
"""
1029

1130

12-
class IdempotencyItemNotFoundError(Exception):
31+
class IdempotencyItemNotFoundError(BaseError):
1332
"""
1433
Item does not exist in persistence store
1534
"""
1635

1736

18-
class IdempotencyAlreadyInProgressError(Exception):
37+
class IdempotencyAlreadyInProgressError(BaseError):
1938
"""
2039
Execution with idempotency key is already in progress
2140
"""
2241

2342

24-
class IdempotencyInvalidStatusError(Exception):
43+
class IdempotencyInvalidStatusError(BaseError):
2544
"""
2645
An invalid status was provided
2746
"""
2847

2948

30-
class IdempotencyValidationError(Exception):
49+
class IdempotencyValidationError(BaseError):
3150
"""
3251
Payload does not match stored idempotency record
3352
"""
3453

3554

36-
class IdempotencyInconsistentStateError(Exception):
55+
class IdempotencyInconsistentStateError(BaseError):
3756
"""
3857
State is inconsistent across multiple requests to persistence store
3958
"""
4059

4160

42-
class IdempotencyPersistenceLayerError(Exception):
61+
class IdempotencyPersistenceLayerError(BaseError):
4362
"""
4463
Unrecoverable error from the data store
4564
"""
4665

4766

48-
class IdempotencyKeyError(Exception):
67+
class IdempotencyKeyError(BaseError):
4968
"""
5069
Payload does not contain an idempotent key
5170
"""

tests/functional/idempotency/test_idempotency.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,13 @@ def test_idempotent_lambda_save_inprogress_error(persistence_store: DynamoDBPers
10701070
# GIVEN a miss configured persistence layer
10711071
# like no table was created for the idempotency persistence layer
10721072
stubber = stub.Stubber(persistence_store.table.meta.client)
1073-
stubber.add_client_error("put_item", "ResourceNotFoundException")
1073+
service_error_code = "ResourceNotFoundException"
1074+
service_message = "Custom message"
1075+
stubber.add_client_error(
1076+
"put_item",
1077+
service_error_code,
1078+
service_message,
1079+
)
10741080
stubber.activate()
10751081

10761082
@idempotent(persistence_store=persistence_store)
@@ -1083,9 +1089,14 @@ def lambda_handler(event, context):
10831089
lambda_handler({}, lambda_context)
10841090

10851091
# THEN idempotent should raise an IdempotencyPersistenceLayerError
1092+
# AND append downstream exception details
10861093
stubber.assert_no_pending_responses()
10871094
stubber.deactivate()
1088-
assert "Failed to save in progress record to idempotency store" == e.value.args[0]
1095+
assert "Failed to save in progress record to idempotency store" in e.value.args[0]
1096+
assert (
1097+
f": (An error occurred ({service_error_code}) when calling the PutItem operation: {service_message})"
1098+
in e.value.args[0]
1099+
)
10891100

10901101

10911102
def test_handler_raise_idempotency_key_error(persistence_store: DynamoDBPersistenceLayer, lambda_context):

0 commit comments

Comments
 (0)