From 6fdb62a0a541527ed324b71ff5a7c705636cdae0 Mon Sep 17 00:00:00 2001 From: Maksymilian Babarowski Date: Fri, 8 Oct 2021 09:45:10 +0200 Subject: [PATCH] dynamodb2 - updated error message when UpdateItem with ConditionalExpression fails (#4375) --- moto/dynamodb2/responses.py | 4 +--- tests/test_dynamodb2/test_dynamodb.py | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index 486c000e5..2ceeb2f9f 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -855,9 +855,7 @@ class DynamoHandler(BaseResponse): return self.error(er, mve.exception_msg) except ValueError: er = "com.amazonaws.dynamodb.v20111205#ConditionalCheckFailedException" - return self.error( - er, "A condition specified in the operation could not be evaluated." - ) + return self.error(er, "The conditional request failed") except TypeError: er = "com.amazonaws.dynamodb.v20111205#ValidationException" return self.error(er, "Validation Exception") diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index d7ed12528..a9dc1e9a2 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -2877,7 +2877,7 @@ def test_condition_expressions(): ExpressionAttributeValues={":match": {"S": "match"}}, ) - with pytest.raises(client.exceptions.ConditionalCheckFailedException): + with pytest.raises(client.exceptions.ConditionalCheckFailedException) as exc: client.update_item( TableName="test1", Key={"client": {"S": "client1"}, "app": {"S": "app1"}}, @@ -2886,6 +2886,18 @@ def test_condition_expressions(): ExpressionAttributeValues={":match": {"S": "match"}}, ExpressionAttributeNames={"#existing": "existing", "#match": "match"}, ) + _assert_conditional_check_failed_exception(exc) + + with pytest.raises(client.exceptions.ConditionalCheckFailedException) as exc: + client.update_item( + TableName="test1", + Key={"client": {"S": "client2"}, "app": {"S": "app1"}}, + UpdateExpression="set #match=:match", + ConditionExpression="attribute_exists(#existing)", + ExpressionAttributeValues={":match": {"S": "match"}}, + ExpressionAttributeNames={"#existing": "existing", "#match": "match"}, + ) + _assert_conditional_check_failed_exception(exc) with pytest.raises(client.exceptions.ConditionalCheckFailedException): client.delete_item( @@ -2897,6 +2909,12 @@ def test_condition_expressions(): ) +def _assert_conditional_check_failed_exception(exc): + err = exc.value.response["Error"] + err["Code"].should.equal("ConditionalCheckFailedException") + err["Message"].should.equal("The conditional request failed") + + @mock_dynamodb2 def test_condition_expression_numerical_attribute(): dynamodb = boto3.resource("dynamodb", region_name="us-east-1") @@ -2972,8 +2990,9 @@ def test_condition_expression__attr_doesnt_exist(): update_if_attr_doesnt_exist() # Second time should fail - with pytest.raises(client.exceptions.ConditionalCheckFailedException): + with pytest.raises(client.exceptions.ConditionalCheckFailedException) as exc: update_if_attr_doesnt_exist() + _assert_conditional_check_failed_exception(exc) @mock_dynamodb2 @@ -3012,7 +3031,7 @@ def test_condition_expression__and_order(): # ensure that the RHS of the AND expression is not evaluated if the LHS # returns true (as it would result an error) - with pytest.raises(client.exceptions.ConditionalCheckFailedException): + with pytest.raises(client.exceptions.ConditionalCheckFailedException) as exc: client.update_item( TableName="test", Key={"forum_name": {"S": "the-key"}}, @@ -3021,6 +3040,7 @@ def test_condition_expression__and_order(): ExpressionAttributeNames={"#ttl": "ttl"}, ExpressionAttributeValues={":ttl": {"N": "6"}, ":old_ttl": {"N": "5"}}, ) + _assert_conditional_check_failed_exception(exc) @mock_dynamodb2