mirror of
https://github.com/matrix-org/pantalaimon.git
synced 2025-02-26 01:21:09 -05:00
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:
parent
3a1b001244
commit
fa3e11a1ec
@ -32,6 +32,7 @@ from pantalaimon.client import (InvalidLimit, InvalidOrderByError, PanClient,
|
|||||||
UnknownRoomError)
|
UnknownRoomError)
|
||||||
from pantalaimon.log import logger
|
from pantalaimon.log import logger
|
||||||
from pantalaimon.store import ClientInfo, PanStore
|
from pantalaimon.store import ClientInfo, PanStore
|
||||||
|
from pantalaimon.index import InvalidQueryError
|
||||||
from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage,
|
from pantalaimon.thread_messages import (AcceptSasMessage, CancelSasMessage,
|
||||||
CancelSendingMessage,
|
CancelSendingMessage,
|
||||||
ConfirmSasMessage, DaemonResponse,
|
ConfirmSasMessage, DaemonResponse,
|
||||||
@ -943,7 +944,7 @@ class ProxyDaemon:
|
|||||||
},
|
},
|
||||||
status=400,
|
status=400,
|
||||||
)
|
)
|
||||||
except (InvalidOrderByError, InvalidLimit) as e:
|
except (InvalidOrderByError, InvalidLimit, InvalidQueryError) as e:
|
||||||
return web.json_response(
|
return web.json_response(
|
||||||
{
|
{
|
||||||
"errcode": "M_INVALID_PARAM",
|
"errcode": "M_INVALID_PARAM",
|
||||||
|
@ -29,6 +29,10 @@ from peewee import (SQL, DateTimeField, ForeignKeyField, Model, SqliteDatabase,
|
|||||||
from pantalaimon.store import use_database
|
from pantalaimon.store import use_database
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidQueryError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DictField(TextField):
|
class DictField(TextField):
|
||||||
def python_value(self, value): # pragma: no cover
|
def python_value(self, value): # pragma: no cover
|
||||||
return json.loads(value)
|
return json.loads(value)
|
||||||
@ -284,12 +288,17 @@ class Searcher:
|
|||||||
# This currently supports only a single room since the query parser
|
# This currently supports only a single room since the query parser
|
||||||
# doesn't seem to work with multiple room fields here.
|
# doesn't seem to work with multiple room fields here.
|
||||||
if room:
|
if room:
|
||||||
search_term = "{} AND room:{}".format(
|
query_string = "{} AND room:{}".format(
|
||||||
search_term,
|
search_term,
|
||||||
sanitize_room_id(room)
|
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:
|
if order_by_recent:
|
||||||
collector = tantivy.TopDocs(max_results,
|
collector = tantivy.TopDocs(max_results,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user