From e8cd02617d83e90b6aceedbf24570d5d80be6ef0 Mon Sep 17 00:00:00 2001 From: Maksymilian Babarowski Date: Wed, 13 Oct 2021 12:36:16 +0200 Subject: [PATCH] Fixed a bug where calling batch_get_item with non-existing tables names would not raise ResourceNotFoundException (#4344) (#4403) --- moto/dynamodb2/responses.py | 12 +++++++++--- tests/test_dynamodb2/test_dynamodb_exceptions.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index 14f4ad3af..425815f12 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -486,9 +486,15 @@ class DynamoHandler(BaseResponse): results["Responses"][table_name] = [] for key in keys: - item = self.dynamodb_backend.get_item( - table_name, key, projection_expression - ) + try: + item = self.dynamodb_backend.get_item( + table_name, key, projection_expression + ) + except ValueError: + return self.error( + "com.amazonaws.dynamodb.v20111205#ResourceNotFoundException", + "Requested resource not found", + ) if item: item_describe = item.describe_attrs(attributes_to_get) results["Responses"][table_name].append(item_describe["Item"]) diff --git a/tests/test_dynamodb2/test_dynamodb_exceptions.py b/tests/test_dynamodb2/test_dynamodb_exceptions.py index e67841805..929601430 100644 --- a/tests/test_dynamodb2/test_dynamodb_exceptions.py +++ b/tests/test_dynamodb2/test_dynamodb_exceptions.py @@ -171,6 +171,18 @@ def test_update_item_range_key_set(): ) +@mock_dynamodb2 +def test_batch_get_item_non_existing_table(): + + client = boto3.client("dynamodb", region_name="us-west-2") + + with pytest.raises(client.exceptions.ResourceNotFoundException) as exc: + client.batch_get_item(RequestItems={"my-table": {"Keys": [{"id": {"N": "0"}}]}}) + err = exc.value.response["Error"] + assert err["Code"].should.equal("ResourceNotFoundException") + assert err["Message"].should.equal("Requested resource not found") + + @mock_dynamodb2 def test_batch_write_item_non_existing_table(): client = boto3.client("dynamodb", region_name="us-west-2")