From 999de45bad934654a7b1f1d874d08fbe0922660f Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 31 Jan 2021 10:32:36 +0100 Subject: buffers/search: fix handling malformed queries --- alot/buffers/search.py | 26 +++++++++++++++----------- alot/db/errors.py | 4 ++++ alot/db/manager.py | 13 ++++++++----- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/alot/buffers/search.py b/alot/buffers/search.py index bf758c95..20c0ae47 100644 --- a/alot/buffers/search.py +++ b/alot/buffers/search.py @@ -5,9 +5,9 @@ import asyncio import urwid -from notmuch2 import NotmuchError from .buffer import Buffer +from ..db.errors import QueryError from ..db.sort import NAME as SORT_NAME from ..settings.const import settings from ..widgets.search import ThreadlineWidget @@ -163,20 +163,24 @@ class SearchBuffer(Buffer): self._result_count = None self._thread_count = None + threads = self.dbman.get_threads(self.querystring, self.sort_order, + exclude_tags) + threadlist = IterWalker(threads, self.dbman) + + # check that the query is well-formed try: - threads = self.dbman.get_threads(self.querystring, self.sort_order, - exclude_tags) - except NotmuchError: + threadlist[0] + except IndexError: + # empty result list, no problem + pass + except QueryError: self.ui.notify('malformed query string: %s' % self.querystring, 'error') - self.listbox = urwid.ListBox([]) - self.body = self.listbox - return - - self.threadlist = IterWalker(threads, self.dbman) + threadlist = [] - self.listbox = urwid.ListBox(self.threadlist) - self.body = self.listbox + self.threadlist = threadlist + self.listbox = urwid.ListBox(self.threadlist) + self.body = self.listbox def get_selected_threadline(self): """ diff --git a/alot/db/errors.py b/alot/db/errors.py index bd2564f9..d5fd9f9a 100644 --- a/alot/db/errors.py +++ b/alot/db/errors.py @@ -12,6 +12,10 @@ class DatabaseROError(DatabaseError): """cannot write to read-only database""" pass +class QueryError(DatabaseError): + """malformed query""" + pass + class NonexistantObjectError(DatabaseError): diff --git a/alot/db/manager.py b/alot/db/manager.py index e57af181..aa67ed40 100644 --- a/alot/db/manager.py +++ b/alot/db/manager.py @@ -11,9 +11,8 @@ import os from notmuch2 import Database, NotmuchError -from .errors import DatabaseError -from .errors import DatabaseROError -from .errors import NonexistantObjectError +from .errors import (DatabaseError, DatabaseROError, NonexistantObjectError, + QueryError) from .sort import ORDER from .thread import Thread from ..settings.const import settings @@ -214,8 +213,12 @@ class DBManager: with self._db_ro() as db: exclude_tags = self._exclude_tags | exclude_tags - for t in db.threads(querystring, sort = sort, exclude_tags = exclude_tags): - yield t.threadid + try: + for t in db.threads(querystring, sort = sort, + exclude_tags = exclude_tags): + yield t.threadid + except NotmuchError as e: + raise QueryError from e async def startup(self): self._write_queue = asyncio.Queue() -- cgit v1.2.3