Skip to content

Commit

Permalink
Improve error reporting for missing index
Browse files Browse the repository at this point in the history
  • Loading branch information
shmygol committed Dec 12, 2019
1 parent 3a42079 commit 704a121
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 6 additions & 3 deletions moto/dynamodb2/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,12 @@ def query(self):
all_indexes = (table.global_indexes or []) + (table.indexes or [])
indexes_by_name = dict((i["IndexName"], i) for i in all_indexes)
if index_name not in indexes_by_name:
raise ValueError(
"Invalid index: %s for table: %s. Available indexes are: %s"
% (index_name, name, ", ".join(indexes_by_name.keys()))
er = "com.amazonaws.dynamodb.v20120810#ResourceNotFoundException"
return self.error(
er,
"Invalid index: {} for table: {}. Available indexes are: {}".format(
index_name, name, ", ".join(indexes_by_name.keys())
),
)

index = indexes_by_name[index_name]["KeySchema"]
Expand Down
5 changes: 3 additions & 2 deletions tests/test_dynamodb2/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2655,14 +2655,15 @@ def test_query_by_non_exists_index():
],
)

with assert_raises(ValueError) as ex:
with assert_raises(ClientError) as ex:
dynamodb.query(
TableName="test",
IndexName="non_exists_index",
KeyConditionExpression="CarModel=M",
)

str(ex.exception).should.equal(
ex.exception.response["Error"]["Code"].should.equal("ResourceNotFoundException")
ex.exception.response["Error"]["Message"].should.equal(
"Invalid index: non_exists_index for table: test. Available indexes are: test_gsi"
)

Expand Down

0 comments on commit 704a121

Please sign in to comment.