From d62024d1b12bd7da096e8802fa123c908806ca5c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 24 Sep 2018 13:35:51 +0530 Subject: [PATCH] Fix error reporting in the new viewer --- src/calibre/gui2/viewer2/web_view.py | 8 ++++++- src/pyj/viewer-main.pyj | 31 ++++++++++++---------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/calibre/gui2/viewer2/web_view.py b/src/calibre/gui2/viewer2/web_view.py index 070c1fd6f1..595442b1ec 100644 --- a/src/calibre/gui2/viewer2/web_view.py +++ b/src/calibre/gui2/viewer2/web_view.py @@ -141,7 +141,13 @@ class WebPage(QWebEnginePage): self.bridge = ViewerBridge(self) def javaScriptConsoleMessage(self, level, msg, linenumber, source_id): - prints('%s:%s: %s' % (source_id, linenumber, msg)) + if level >= QWebEnginePage.ErrorMessageLevel and source_id == 'userscript:viewer.js': + error_dialog(self.parent(), _('Unhandled error'), _( + 'There was an unhandled error: {} at line: {} of {}').format( + msg, linenumber, source_id.partition(':')[2]), show=True) + prefix = {QWebEnginePage.InfoMessageLevel: 'INFO', QWebEnginePage.WarningMessageLevel: 'WARNING'}.get( + level, 'ERROR') + prints('%s: %s:%s: %s' % (prefix, source_id, linenumber, msg)) def acceptNavigationRequest(self, url, req_type, is_main_frame): if req_type == self.NavigationTypeReload: diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index ffe5195d80..914653cb5a 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -9,7 +9,7 @@ from gettext import gettext as _ import initialize # noqa: unused-import from ajax import ajax from book_list.globals import set_session_data -from modals import error_dialog +from modals import error_dialog, create_modal_container from qt import from_python, to_python from read_book.db import new_book from read_book.globals import runtime, ui_operations @@ -45,12 +45,13 @@ def manifest_received(key, end_type, xhr, ev): nonlocal book if end_type is 'load': book = new_book(key, {}) - book.manifest = xhr.response + book.manifest = JSON.parse(xhr.responseText) book.metadata = book.manifest.metadata book.stored_files = {} book.is_complete = True v'delete book.manifest["metadata"]' v'delete book.manifest["last_read_positions"]' + view.display_book(book) else: error_dialog(_('Could not open book'), _( 'Failed to load book manifest, click "Show details" for more info'), @@ -99,23 +100,16 @@ def start_book_load(key, prefs): def onerror(msg, script_url, line_number, column_number, error_object): - if error_object is None: - # This happens for cross-domain errors (probably javascript injected - # into the browser via extensions/ userscripts and the like). It also - # happens all the time when using Chrom on Safari, so ignore this - # type of error - console.log(f'Unhandled error from external javascript, ignoring: {msg} {script_url} {line_number}') - return + if not error_object: + # cross domain error + return False + fname = script_url.rpartition('/')[-1] or script_url + msg += '
' + 'Error at {}:{}:{}'.format(fname, line_number, column_number or '') + '' + details = '' console.log(error_object) - try: - fname = script_url.rpartition('/')[-1] or script_url - msg = msg + '
' + 'Error at {}:{}:{}'.format(fname, line_number, column_number or '') + '' - details = '' - details = traceback.format_exception(error_object).join('') - error_dialog(_('Unhandled error'), msg, details) - return True - except: - console.log('There was an error in the unhandled exception handler') + details = traceback.format_exception(error_object).join('') + error_dialog(_('Unhandled error'), msg, details) + return True if window is window.top: @@ -126,6 +120,7 @@ if window is window.top: ui_operations.show_error = show_error document.body.appendChild(E.div(id='view')) window.onerror = onerror + create_modal_container() else: # iframe pass