Skip to content

Commit 35e6424

Browse files
authored
DynamoDB: Bring parity with AWS in error messages (#10198)
1 parent 5b6aa9d commit 35e6424

7 files changed

Lines changed: 86 additions & 67 deletions

File tree

‎moto/dynamodb/exceptions.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ def __init__(self, attribute_value: str):
122122

123123

124124
class ExpressionAttributeValuesEmpty(MockValidationException):
125-
def __init__(self) -> None:
126-
super().__init__("ExpressionAttributeValues must not be empty")
125+
def __init__(self, validation_error_prefix: bool = False) -> None:
126+
msg = "ExpressionAttributeValues must not be empty"
127+
if validation_error_prefix:
128+
# Error messages are inconsistent - some operations have this prefix, others (like Transact* operations) do not
129+
msg = f"1 validation error detected: {msg}"
130+
super().__init__(msg)
127131

128132

129133
class UpdateExprSyntaxError(InvalidUpdateExpression):
@@ -216,7 +220,7 @@ def __init__(self, msg: str | None = None, item: dict[str, Any] | None = None):
216220
"__type": ConditionalCheckFailed.error_type,
217221
# Note the uppercase Message
218222
# This ensures the message is only part of the 'error': {'message': .., 'code': ..}
219-
"Message": _msg,
223+
"message": _msg,
220224
"Item": item,
221225
}
222226
)
@@ -299,24 +303,24 @@ def __init__(self, name: str | None, expected_type: str | None, actual_type: str
299303
super().__init__(self.msg.format(name, expected_type, actual_type))
300304

301305

302-
class DuplicateUpdateExpression(InvalidUpdateExpression):
306+
class DuplicateUpdateExpression(MockValidationException):
303307
def __init__(self, name_1: str, name_2: str):
304308
super().__init__(
305-
f"Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [{', '.join(name_1.split('.'))}], path two: [{', '.join(name_2.split('.'))}]"
309+
f"1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [{', '.join(name_1.split('.'))}], path two: [{', '.join(name_2.split('.'))}]"
306310
)
307311

308312

309313
class InvalidProjectionExpression(MockValidationException):
310314
def __init__(self, path_1: str, path_2: str):
311315
super().__init__(
312-
f"Invalid ProjectionExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [{', '.join(path_1.split('.'))}], path two: [{', '.join(path_2.split('.'))}]"
316+
f"1 validation error detected: Invalid ProjectionExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [{', '.join(path_1.split('.'))}], path two: [{', '.join(path_2.split('.'))}]"
313317
)
314318

315319

316-
class TooManyClauses(InvalidUpdateExpression):
320+
class TooManyClauses(MockValidationException):
317321
def __init__(self, _type: str) -> None:
318322
super().__init__(
319-
f'The "{_type}" section can only be used once in an update expression;'
323+
f'1 validation error detected: Invalid UpdateExpression: The "{_type}" section can only be used once in an update expression;'
320324
)
321325

322326

‎moto/dynamodb/parsing/executors.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def execute(self, item: Item) -> None:
227227
if value_to_add.is_set():
228228
if len(value_to_add.value) == 0:
229229
raise MockValidationException(
230-
"ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An string set may not be empty"
230+
"1 validation error detected: One or more parameter values were invalid: An string set may not be empty"
231231
)
232232
try:
233233
current_string_set = self.get_item_at_end_of_path(item)

‎moto/dynamodb/responses.py‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _validate_attr(attr: dict[str, Any]) -> None:
137137
for set_type, error in [("NS", "number"), ("SS", "string")]:
138138
if set_type in attr and attr[set_type] == []:
139139
raise MockValidationException(
140-
f"One or more parameter values were invalid: An {error} set may not be empty"
140+
f"1 validation error detected: One or more parameter values were invalid: An {error} set may not be empty"
141141
)
142142

143143
else:
@@ -165,12 +165,14 @@ def validate_attributes_used(
165165
attribute_names: dict[str, Any] | None,
166166
names_used: list[str],
167167
provided_attr: str = "Names",
168+
validation_error_prefix: bool = True,
168169
) -> None:
169170
for name in attribute_names or []:
170171
if name not in names_used:
171-
raise MockValidationException(
172-
f"Value provided in ExpressionAttribute{provided_attr} unused in expressions: keys: {{{name}}}"
173-
)
172+
msg = f"Value provided in ExpressionAttribute{provided_attr} unused in expressions: keys: {{{name}}}"
173+
if validation_error_prefix:
174+
msg = f"1 validation error detected: {msg}"
175+
raise MockValidationException(msg)
174176

175177

176178
def validate_select(
@@ -809,7 +811,9 @@ def get_item(self) -> ActionResult:
809811
projection_expressions = parser.parse()
810812

811813
validate_attributes_used(
812-
expression_attribute_names, parser.expr_attr_names_found
814+
expression_attribute_names,
815+
parser.expr_attr_names_found,
816+
validation_error_prefix=False,
813817
)
814818

815819
item = self.dynamodb_backend.get_item(name, key, projection_expressions)
@@ -864,7 +868,9 @@ def batch_get_item(self) -> ActionResult:
864868
)
865869
projection_expressions = parser.parse()
866870
validate_attributes_used(
867-
expression_attribute_names, parser.expr_attr_names_found
871+
expression_attribute_names,
872+
parser.expr_attr_names_found,
873+
validation_error_prefix=False,
868874
)
869875

870876
results["Responses"][table_name] = []
@@ -1012,7 +1018,9 @@ def query(self) -> ActionResult:
10121018
filter_kwargs.update(query_filters)
10131019

10141020
validate_attributes_used(
1015-
expression_attribute_names, expression_attribute_names_used
1021+
expression_attribute_names,
1022+
expression_attribute_names_used,
1023+
validation_error_prefix=False,
10161024
)
10171025
index_name = self.body.get("IndexName")
10181026
exclusive_start_key = self.body.get("ExclusiveStartKey")
@@ -1120,7 +1128,9 @@ def scan(self) -> ActionResult:
11201128
)
11211129

11221130
validate_attributes_used(
1123-
expression_attribute_names, expression_attribute_names_used
1131+
expression_attribute_names,
1132+
expression_attribute_names_used,
1133+
validation_error_prefix=False,
11241134
)
11251135

11261136
validate_select(
@@ -1227,7 +1237,7 @@ def update_item(self) -> ActionResult:
12271237
update_expression = update_expression.strip()
12281238
if update_expression == "":
12291239
raise MockValidationException(
1230-
"Invalid UpdateExpression: The expression can not be empty;"
1240+
"1 validation error detected: Invalid UpdateExpression: The expression can not be empty;"
12311241
)
12321242
update_expression_ast = UpdateExpressionParser.make(update_expression)
12331243
attr_name_clauses = update_expression_ast.find_clauses(
@@ -1341,15 +1351,15 @@ def _get_expr_attr_values(self) -> dict[str, dict[str, str]]:
13411351
if values is None:
13421352
return {}
13431353
if len(values) == 0:
1344-
raise ExpressionAttributeValuesEmpty
1354+
raise ExpressionAttributeValuesEmpty(validation_error_prefix=True)
13451355
for key in values:
13461356
if not key.startswith(":"):
13471357
raise MockValidationException(
13481358
f'ExpressionAttributeValues contains invalid key: Syntax error; key: "{key}"'
13491359
)
13501360
if values[key] == {"NS": []}:
13511361
raise MockValidationException(
1352-
f"ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An number set may not be empty for key {key}"
1362+
"1 validation error detected: One or more parameter values were invalid: An number set may not be empty"
13531363
)
13541364
return values
13551365

@@ -1512,7 +1522,9 @@ def transact_write_items(self) -> ActionResult:
15121522
]
15131523

15141524
validate_attributes_used(
1515-
expression_attribute_names, expression_attribute_names_used
1525+
expression_attribute_names,
1526+
expression_attribute_names_used,
1527+
validation_error_prefix=False,
15161528
)
15171529

15181530
self.dynamodb_backend.transact_write_items(transact_items)

‎tests/test_dynamodb/exceptions/test_dynamodb_exceptions.py‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_multiple_add_clauses(self):
281281
assert err["Code"] == "ValidationException"
282282
assert (
283283
err["Message"]
284-
== 'Invalid UpdateExpression: The "ADD" section can only be used once in an update expression;'
284+
== '1 validation error detected: Invalid UpdateExpression: The "ADD" section can only be used once in an update expression;'
285285
)
286286

287287
def test_multiple_remove_clauses(self):
@@ -296,7 +296,7 @@ def test_multiple_remove_clauses(self):
296296
assert err["Code"] == "ValidationException"
297297
assert (
298298
err["Message"]
299-
== 'Invalid UpdateExpression: The "REMOVE" section can only be used once in an update expression;'
299+
== '1 validation error detected: Invalid UpdateExpression: The "REMOVE" section can only be used once in an update expression;'
300300
)
301301

302302
def test_multiple_set_clauses(self):
@@ -310,7 +310,7 @@ def test_multiple_set_clauses(self):
310310
assert err["Code"] == "ValidationException"
311311
assert (
312312
err["Message"]
313-
== 'Invalid UpdateExpression: The "SET" section can only be used once in an update expression;'
313+
== '1 validation error detected: Invalid UpdateExpression: The "SET" section can only be used once in an update expression;'
314314
)
315315

316316
def test_delete_and_add_on_same_set(self):
@@ -323,7 +323,7 @@ def test_delete_and_add_on_same_set(self):
323323
)
324324
assert exc.value.response["Error"]["Code"] == "ValidationException"
325325
assert exc.value.response["Error"]["Message"].startswith(
326-
"Invalid UpdateExpression: Two document paths overlap with each other;"
326+
"1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other;"
327327
)
328328

329329
def test_remove_and_add_on_same_set(self):
@@ -336,7 +336,7 @@ def test_remove_and_add_on_same_set(self):
336336
)
337337
assert exc.value.response["Error"]["Code"] == "ValidationException"
338338
assert exc.value.response["Error"]["Message"].startswith(
339-
"Invalid UpdateExpression: Two document paths overlap with each other;"
339+
"1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other;"
340340
)
341341

342342
def test_set_and_remove_on_overlapping_paths(self):
@@ -348,7 +348,7 @@ def test_set_and_remove_on_overlapping_paths(self):
348348
)
349349
assert exc.value.response["Error"]["Code"] == "ValidationException"
350350
assert exc.value.response["Error"]["Message"].startswith(
351-
"Invalid UpdateExpression: Two document paths overlap with each other;"
351+
"1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other;"
352352
)
353353

354354
def test_set_path_overlap(self):
@@ -360,7 +360,7 @@ def test_set_path_overlap(self):
360360
)
361361
assert exc.value.response["Error"]["Code"] == "ValidationException"
362362
assert exc.value.response["Error"]["Message"].startswith(
363-
"Invalid UpdateExpression: Two document paths overlap with each other;"
363+
"1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other;"
364364
)
365365

366366
def test_path_overlap_error_uses_expression_attribute_name(self):
@@ -374,7 +374,7 @@ def test_path_overlap_error_uses_expression_attribute_name(self):
374374
assert exc.value.response["Error"]["Code"] == "ValidationException"
375375
assert (
376376
exc.value.response["Error"]["Message"]
377-
== "Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [string_set], path two: [string_set]"
377+
== "1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [string_set], path two: [string_set]"
378378
)
379379

380380
def test_path_overlap_error_splits_paths(self):
@@ -387,7 +387,7 @@ def test_path_overlap_error_splits_paths(self):
387387
assert exc.value.response["Error"]["Code"] == "ValidationException"
388388
assert (
389389
exc.value.response["Error"]["Message"]
390-
== "Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [string_set, nested], path two: [string_set, nested]"
390+
== "1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths; path one: [string_set, nested], path two: [string_set, nested]"
391391
)
392392

393393
# pretty weird AWS behavior with the next two tests, seems like a bug
@@ -416,7 +416,7 @@ def test_overlapping_paths_error_if_identical_paths_are_expression_attribute_nam
416416
)
417417
assert exc.value.response["Error"]["Code"] == "ValidationException"
418418
assert exc.value.response["Error"]["Message"].startswith(
419-
"Invalid UpdateExpression: Two document paths overlap with each other;"
419+
"1 validation error detected: Invalid UpdateExpression: Two document paths overlap with each other;"
420420
)
421421

422422
def test_update_item_with_shared_root_but_different_overall(self):
@@ -471,7 +471,7 @@ def test_update_item_unused_attributes(table_name=None):
471471
assert err["Code"] == "ValidationException"
472472
assert (
473473
err["Message"]
474-
== "Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
474+
== "1 validation error detected: Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
475475
)
476476

477477
# Unused Name (count) only
@@ -486,7 +486,7 @@ def test_update_item_unused_attributes(table_name=None):
486486
assert err["Code"] == "ValidationException"
487487
assert (
488488
err["Message"]
489-
== "Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
489+
== "1 validation error detected: Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
490490
)
491491

492492
# Unused Value (countChange)
@@ -501,7 +501,7 @@ def test_update_item_unused_attributes(table_name=None):
501501
assert err["Code"] == "ValidationException"
502502
assert (
503503
err["Message"]
504-
== "Value provided in ExpressionAttributeValues unused in expressions: keys: {:countChange}"
504+
== "1 validation error detected: Value provided in ExpressionAttributeValues unused in expressions: keys: {:countChange}"
505505
)
506506

507507
# Used value in ConditionExpression
@@ -531,7 +531,7 @@ def test_update_item_unused_attributes(table_name=None):
531531
assert err["Code"] == "ValidationException"
532532
assert (
533533
err["Message"]
534-
== "ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An number set may not be empty for key :one"
534+
== "1 validation error detected: One or more parameter values were invalid: An number set may not be empty"
535535
)
536536

537537

@@ -552,7 +552,7 @@ def test_put_item_unused_attributes(table_name=None):
552552
assert err["Code"] == "ValidationException"
553553
assert (
554554
err["Message"]
555-
== "Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
555+
== "1 validation error detected: Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
556556
)
557557

558558

@@ -640,7 +640,7 @@ def test_delete_unused_attribute_name(table_name=None):
640640
assert err["Code"] == "ValidationException"
641641
assert (
642642
err["Message"]
643-
== "Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
643+
== "1 validation error detected: Value provided in ExpressionAttributeNames unused in expressions: keys: {#count}"
644644
)
645645

646646

@@ -1374,7 +1374,7 @@ def test_put_item_empty_set(table_name=None):
13741374
assert err["Code"] == "ValidationException"
13751375
assert (
13761376
err["Message"]
1377-
== "One or more parameter values were invalid: An number set may not be empty"
1377+
== "1 validation error detected: One or more parameter values were invalid: An number set may not be empty"
13781378
)
13791379

13801380
with pytest.raises(ClientError) as exc:
@@ -1385,7 +1385,7 @@ def test_put_item_empty_set(table_name=None):
13851385
assert err["Code"] == "ValidationException"
13861386
assert (
13871387
err["Message"]
1388-
== "One or more parameter values were invalid: An string set may not be empty"
1388+
== "1 validation error detected: One or more parameter values were invalid: An string set may not be empty"
13891389
)
13901390

13911391

@@ -1811,15 +1811,16 @@ def test_list_append_errors_for_unknown_attribute_value():
18111811
)
18121812

18131813

1814-
@mock_aws
1815-
def test_query_with_empty_filter_expression():
1816-
ddb = boto3.resource("dynamodb", region_name="us-east-1")
1817-
table = ddb.create_table(
1818-
TableName=f"T{uuid4()}", BillingMode="PAY_PER_REQUEST", **table_schema
1819-
)
1814+
@pytest.mark.aws_verified
1815+
@dynamodb_aws_verified()
1816+
def test_query_with_empty_filter_expression(table_name=None):
1817+
table = boto3.resource("dynamodb", region_name="us-east-1").Table(table_name)
1818+
18201819
with pytest.raises(ClientError) as exc:
18211820
table.query(
1822-
KeyConditionExpression="partitionKey = sth", ProjectionExpression=""
1821+
KeyConditionExpression="pk = :pk",
1822+
ExpressionAttributeValues={":pk": "pk"},
1823+
ProjectionExpression="",
18231824
)
18241825
err = exc.value.response["Error"]
18251826
assert err["Code"] == "ValidationException"
@@ -1940,7 +1941,7 @@ def _assert_with_return_values(self, exc):
19401941
"Message": "The conditional request failed",
19411942
"Code": "ConditionalCheckFailedException",
19421943
}
1943-
assert "message" not in resp
1944+
assert resp["message"] == "The conditional request failed"
19441945
assert resp["Item"] == {
19451946
"pk": {"S": "the-key"},
19461947
"body": {"S": "some test msg"},

‎tests/test_dynamodb/exceptions/test_dynamodb_transactions.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ def test_transact_write_items__update_with_multiple_set_clauses(table_name=None)
151151
)
152152
err = exc.value.response["Error"]
153153
assert err["Code"] == "ValidationException"
154+
# In AWS, this is the full error message
155+
# Moto prefixes this error with '1 validation error detected', as that is what other methods also return.
156+
# Hopefully AWS streamlines the error messages soon to include that prefix everywhere
154157
assert (
155-
err["Message"]
156-
== 'Invalid UpdateExpression: The "SET" section can only be used once in an update expression;'
158+
'Invalid UpdateExpression: The "SET" section can only be used once in an update expression;'
159+
in err["Message"]
157160
)
158161

159162
# Note that we can do this by simply separating the statements with a comma

0 commit comments

Comments
 (0)