From 9f1d9a9781f60b4e0ded9bd567453ab35e8d579f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 17 May 2021 17:06:00 +0530 Subject: [PATCH] Handle errors in the worker --- src/pyj/read_book/search.pyj | 20 ++++++++++++++++++-- src/pyj/read_book/search_worker.pyj | 6 ++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/pyj/read_book/search.pyj b/src/pyj/read_book/search.pyj index c0476b8e25..9d15387d51 100644 --- a/src/pyj/read_book/search.pyj +++ b/src/pyj/read_book/search.pyj @@ -8,9 +8,12 @@ from book_list.theme import get_color from complete import create_search_bar from dom import add_extra_css, build_rule, svgicon from gettext import gettext as _ +from modals import error_dialog from read_book.globals import ui_operations -from read_book.search_worker import worker_main from read_book.resources import text_from_serialized_html +from read_book.search_worker import ( + CONNECT_FAILED, DB_ERROR, GET_SPINE_FAILED, UNHANDLED_ERROR, worker_main +) from read_book.shortcuts import shortcut_for_key_event from worker import start_worker @@ -67,10 +70,23 @@ class SearchOverlay: def on_worker_message(self, evt): msg = evt.data if msg.type is 'error': - pass + details = msg.msg + emsg = _('Unknown error') + if msg.code is GET_SPINE_FAILED: + emsg = _('Loading text from the book failed.') + elif msg.code is CONNECT_FAILED: + emsg = _('Connecting to database storing the local copy of the book failed in the worker thread.') + elif msg.code is UNHANDLED_ERROR: + emsg = _('There was an unhandled error while searching.') + elif msg.code is DB_ERROR: + emsg = msg.error.msg + details = msg.error.details + error_dialog(_('Could not search'), emsg, details) elif msg.id is self.search_in_flight_id: if msg.type is 'search_complete': self.search_in_flight_id = None + elif msg.type is 'search_result': + pass def clear_caches(self): if self._worker: diff --git a/src/pyj/read_book/search_worker.pyj b/src/pyj/read_book/search_worker.pyj index ea863c56d5..88fbd7c4a6 100644 --- a/src/pyj/read_book/search_worker.pyj +++ b/src/pyj/read_book/search_worker.pyj @@ -4,6 +4,7 @@ from __python__ import bound_methods, hash_literals from read_book.db import DB from read_book.resources import text_from_serialized_html +from traceback import format_exception GET_SPINE_FAILED = 1 CONNECT_FAILED = 2 @@ -200,5 +201,6 @@ def worker_main(): elif e.data.type is 'clear_caches': wc.clear_caches() - self.onerror = def(e): - send_error(UNHANDLED_ERROR, f'{e.line}:{e.message}') + self.addEventListener('error', def (e): + send_error(UNHANDLED_ERROR, f'{e.lineno}:{e.message}' + '\n\n' + format_exception(e.error).join('')) + )