Skip to content

Commit

Permalink
DynamoDB: transact_write_items() should return failing item (#5482)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored Sep 17, 2022
1 parent 5739db4 commit e230750
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions moto/dynamodb/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ class TransactionCanceledException(DynamodbException):

def __init__(self, errors):
msg = self.cancel_reason_msg.format(
", ".join([str(code) for code, _ in errors])
", ".join([str(code) for code, _, _ in errors])
)
super().__init__(
error_type=TransactionCanceledException.error_type, message=msg
)
reasons = [
{"Code": code, "Message": message} if code else {"Code": "None"}
for code, message in errors
{"Code": code, "Message": message, **item} if code else {"Code": "None"}
for code, message, item in errors
]
self.description = json.dumps(
{
Expand Down
8 changes: 4 additions & 4 deletions moto/dynamodb/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ def check_unicity(table_name, key):
raise MultipleTransactionsException()
target_items.add(item)

errors = []
errors = [] # [(Code, Message, Item), ..]
for item in transact_items:
try:
if "ConditionCheck" in item:
Expand Down Expand Up @@ -1738,14 +1738,14 @@ def check_unicity(table_name, key):
)
else:
raise ValueError
errors.append((None, None))
errors.append((None, None, None))
except MultipleTransactionsException:
# Rollback to the original state, and reraise the error
self.tables = original_table_state
raise MultipleTransactionsException()
except Exception as e: # noqa: E722 Do not use bare except
errors.append((type(e).__name__, e.message))
if set(errors) != set([(None, None)]):
errors.append((type(e).__name__, e.message, item))
if any([code is not None for code, _, _ in errors]):
# Rollback to the original state, and reraise the errors
self.tables = original_table_state
raise TransactionCanceledException(errors)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_dynamodb/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3882,7 +3882,11 @@ def test_transact_write_items_put_conditional_expressions():
reasons = ex.value.response["CancellationReasons"]
reasons.should.have.length_of(5)
reasons.should.contain(
{"Code": "ConditionalCheckFailed", "Message": "The conditional request failed"}
{
"Code": "ConditionalCheckFailed",
"Message": "The conditional request failed",
"Item": {"id": {"S": "foo2"}, "foo": {"S": "bar"}},
}
)
reasons.should.contain({"Code": "None"})
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
Expand Down

0 comments on commit e230750

Please sign in to comment.