index: Catch query parsing errors.

Tantivy's query parser can throw errors if the syntax of the query is
invalid.

There is a more lenient query parser[1] in the works but until then
catch query parser errors and return an error response to the user.

[1] https://github.com/tantivy-search/tantivy/issues/5
This commit is contained in:
Damir Jelić 2019-06-14 15:15:58 +02:00
parent 3a1b001244
commit fa3e11a1ec
2 changed files with 13 additions and 3 deletions

View File

@ -32,6 +32,7 @@ from pantalaimon.client import (InvalidLimit, InvalidOrderByError, PanClient,
UnknownRoomError)
from pantalaimon.log import logger
from pantalaimon.store import ClientInfo, PanStore
from pantalaimon.index import InvalidQueryError
from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage,
CancelSendingMessage,
ConfirmSasMessage, DaemonResponse,
@ -943,7 +944,7 @@ class ProxyDaemon:
},
status=400,
)
except (InvalidOrderByError, InvalidLimit) as e:
except (InvalidOrderByError, InvalidLimit, InvalidQueryError) as e:
return web.json_response(
{
"errcode": "M_INVALID_PARAM",

View File

@ -29,6 +29,10 @@ from peewee import (SQL, DateTimeField, ForeignKeyField, Model, SqliteDatabase,
from pantalaimon.store import use_database
class InvalidQueryError(Exception):
pass
class DictField(TextField):
def python_value(self, value): # pragma: no cover
return json.loads(value)
@ -284,12 +288,17 @@ class Searcher:
# This currently supports only a single room since the query parser
# doesn't seem to work with multiple room fields here.
if room:
search_term = "{} AND room:{}".format(
query_string = "{} AND room:{}".format(
search_term,
sanitize_room_id(room)
)
else:
query_string = search_term
query = queryparser.parse_query(search_term)
try:
query = queryparser.parse_query(query_string)
except ValueError:
raise InvalidQueryError(f"Invalid search term: {search_term}")
if order_by_recent:
collector = tantivy.TopDocs(max_results,