Skip to content

Commit

Permalink
Fix crash on search parse exceptions (fixes #44)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jun 13, 2017
1 parent 9ea6c1a commit 4dfccc2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Changelog
0.2.0 (unreleased)
------------------

- Nothing changed yet.
**Bug fixes**

- Fix crash on search parse exceptions (fixes #44)


0.1.0 (2017-05-26)
Expand Down
8 changes: 6 additions & 2 deletions kinto_elasticsearch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ def search_view(request, **kwargs):

except elasticsearch.RequestError as e:
# Malformed query.
message = e.info["error"]["reason"]
details = e.info["error"]["root_cause"][0]
if isinstance(e.info["error"], dict):
message = e.info["error"]["reason"]
details = e.info["error"]["root_cause"][0]
else:
message = e.info["error"]
details = None
response = http_error(httpexceptions.HTTPBadRequest(),
errno=ERRORS.INVALID_PARAMETERS,
message=message,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,11 @@ def test_can_aggregate_values(self):
{"key": "abc", "doc_count": 1},
{"key": "efg", "doc_count": 1},
]

def test_search_parse_exception_returns_400(self):
e = elasticsearch.RequestError('', '',
{"error": "Could not find aggregator type"})
with mock.patch("kinto_elasticsearch.indexer.Indexer.search",
side_effect=e):
self.app.post_json("/buckets/bid/collections/cid/search",
headers=self.headers, status=400)

0 comments on commit 4dfccc2

Please sign in to comment.