From 613b1395b8535411ecb1fa0093c8c232484f534c Mon Sep 17 00:00:00 2001 From: Brian Pandola Date: Wed, 17 Feb 2021 03:22:00 -0800 Subject: [PATCH] Fix: DynamoDB:GetItem throws wrong error when table doesn't exist (#3700) * Fix: DynamoDB:GetItem throws wrong error when table doesn't exist * Use unique exception for table not found, per PR feedback * Just fix the reported issue, without touching anything else... --- moto/dynamodb2/responses.py | 6 ++++++ tests/test_dynamodb2/test_dynamodb.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py index fe1a463d..5958a38c 100644 --- a/moto/dynamodb2/responses.py +++ b/moto/dynamodb2/responses.py @@ -349,6 +349,12 @@ class DynamoHandler(BaseResponse): def get_item(self): name = self.body["TableName"] + table = self.dynamodb_backend.get_table(name) + if table is None: + return self.error( + "com.amazonaws.dynamodb.v20120810#ResourceNotFoundException", + "Requested resource not found", + ) key = self.body["Key"] projection_expression = self.body.get("ProjectionExpression") expression_attribute_names = self.body.get("ExpressionAttributeNames", {}) diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index 2ad45ddc..446d0238 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -5698,3 +5698,12 @@ def test_update_item_add_to_list_using_legacy_attribute_updates(): resp = table.get_item(Key={"id": "list_add"}) resp["Item"]["attr"].should.equal(["a", "b", "c", "d", "e"]) + + +@mock_dynamodb2 +def test_get_item_for_non_existent_table_raises_error(): + client = boto3.client("dynamodb", "us-east-1") + with pytest.raises(ClientError) as ex: + client.get_item(TableName="non-existent", Key={"site-id": {"S": "foo"}}) + ex.value.response["Error"]["Code"].should.equal("ResourceNotFoundException") + ex.value.response["Error"]["Message"].should.equal("Requested resource not found")