Skip to content

feat(search): Added error handling to issue search. #13457

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

Merged
merged 3 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/sentry/api/event_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def parse_search_query(query):
raise InvalidSearchQuery(
'%s %s' % (
u'Parse error: %r (column %d).' % (e.expr.name, e.column()),
'This is commonly caused by unmatched-parentheses.',
'This is commonly caused by unmatched-parentheses. Enclose any text in double quotes.',
)
)
return SearchVisitor().visit(tree)
Expand Down
11 changes: 10 additions & 1 deletion src/sentry/api/issue_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

from django.utils.functional import cached_property
from parsimonious.exceptions import IncompleteParseError

from sentry.api.event_search import (
event_search_grammar,
Expand Down Expand Up @@ -73,7 +74,15 @@ def visit_boolean_operator(self, node, children):


def parse_search_query(query):
tree = event_search_grammar.parse(query)
try:
tree = event_search_grammar.parse(query)
except IncompleteParseError as e:
raise InvalidSearchQuery(
'%s %s' % (
u'Parse error: %r (column %d).' % (e.expr.name, e.column()),
'This is commonly caused by unmatched-parentheses. Enclose any text in double quotes.',
)
)
return IssueSearchVisitor().visit(tree)


Expand Down
10 changes: 5 additions & 5 deletions tests/sentry/api/test_event_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,25 +923,25 @@ def test_malformed_groups(self):
'(user.email:foo@example.com OR user.email:bar@example.com'
)
assert six.text_type(
error.value) == "Parse error: 'search' (column 1). This is commonly caused by unmatched-parentheses."
error.value) == "Parse error: 'search' (column 1). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."
with pytest.raises(InvalidSearchQuery) as error:
parse_search_query(
'((user.email:foo@example.com OR user.email:bar@example.com AND user.email:bar@example.com)'
)
assert six.text_type(
error.value) == "Parse error: 'search' (column 1). This is commonly caused by unmatched-parentheses."
error.value) == "Parse error: 'search' (column 1). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."
with pytest.raises(InvalidSearchQuery) as error:
parse_search_query(
'user.email:foo@example.com OR user.email:bar@example.com)'
)
assert six.text_type(
error.value) == "Parse error: 'search' (column 57). This is commonly caused by unmatched-parentheses."
error.value) == "Parse error: 'search' (column 57). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."
with pytest.raises(InvalidSearchQuery) as error:
parse_search_query(
'(user.email:foo@example.com OR user.email:bar@example.com AND user.email:bar@example.com))'
)
assert six.text_type(
error.value) == "Parse error: 'search' (column 91). This is commonly caused by unmatched-parentheses."
error.value) == "Parse error: 'search' (column 91). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."

def test_grouping_without_boolean_terms(self):
with pytest.raises(InvalidSearchQuery) as error:
Expand All @@ -954,7 +954,7 @@ def test_grouping_without_boolean_terms(self):
raw_value='undefined is not an object (evaluating "function.name")'),
)]
assert six.text_type(
error.value) == "Parse error: 'search' (column 28). This is commonly caused by unmatched-parentheses."
error.value) == "Parse error: 'search' (column 28). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."


class GetSnubaQueryArgsTest(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/snuba/api/endpoints/test_organization_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_invalid_search_terms(self):
response = self.client.get(url, {'query': 'hi \n there'}, format='json')

assert response.status_code == 400, response.content
assert response.data['detail'] == "Parse error: 'search' (column 4). This is commonly caused by unmatched-parentheses."
assert response.data['detail'] == "Parse error: 'search' (column 4). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."

def test_project_filtering(self):
user = self.create_user(is_staff=False, is_superuser=False)
Expand Down
2 changes: 1 addition & 1 deletion tests/snuba/api/endpoints/test_organization_events_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_invalid_search_terms(self):
response = self.client.get(self.url, {'query': 'hi \n there'}, format='json')

assert response.status_code == 400, response.content
assert response.data['detail'] == "Parse error: 'search' (column 4). This is commonly caused by unmatched-parentheses."
assert response.data['detail'] == "Parse error: 'search' (column 4). This is commonly caused by unmatched-parentheses. Enclose any text in double quotes."

def test_raw_data(self):
self.login_as(user=self.user)
Expand Down