Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: DynamoDB:GetItem throws wrong error when table doesn't exist #3700

Merged
merged 3 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions moto/dynamodb2/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,14 @@ def get_item(self):

try:
item = self.dynamodb_backend.get_item(name, key, projection_expression)
except ValueError:
er = "com.amazon.coral.validate#ValidationException"
return self.error(er, "Validation Exception")
except ValueError as e:
if str(e) == "No table found":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to throw a custom exception when the table does not exist? I feel like this would be a cleaner approach:

try:
...
except TableNotFound:
...
except ValueError:
...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I did initially, but unfortunately it breaks other tests. I opted for the simplest solution, though I agree with you that's it's not the most elegant. Fixing this properly requires fairly major surgery, which I'm pretty hesitant to undertake.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...looked at this again. Decided to just fix the reported issue, without touching anything else. Let me know what you think.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this makes sense, to fail as early as possible. Thanks!

er = "com.amazonaws.dynamodb.v20120810#ResourceNotFoundException"
msg = "Requested resource not found"
else:
er = "com.amazon.coral.validate#ValidationException"
msg = "Validation Exception"
return self.error(er, msg)
if item:
item_dict = item.describe_attrs(attributes=None)
item_dict["ConsumedCapacity"] = {"TableName": name, "CapacityUnits": 0.5}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_dynamodb2/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")