From 95a5ee7b8aadb3c0833854e248690536a2f21e6f Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 2 Aug 2020 14:34:44 +0200 Subject: [PATCH] Set up global error handler and modified error handling for initial GUI build --- nw/__init__.py | 58 ++++++++++++++++++++++++++++++-------------------- nw/guimain.py | 1 + 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/nw/__init__.py b/nw/__init__.py index 2271157a..5ffae592 100644 --- a/nw/__init__.py +++ b/nw/__init__.py @@ -34,6 +34,7 @@ from os import path, remove, rename from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication, QErrorMessage +from nw.error import exceptionHandler from nw.config import Config __package__ = "nw" @@ -249,11 +250,12 @@ def main(sysArgs=None): if errorData: errApp = QApplication([]) errMsg = QErrorMessage() - errMsg.setMinimumWidth(500) - errMsg.setMinimumHeight(300) + errMsg.resize(500, 300) errMsg.showMessage(( - "ERROR: novelWriter cannot start due to the following issues:

" - " - %s

Exiting." + "

A critical error has been encountered

" + "

novelWriter cannot start due to the following issues:

" + "

 - %s

" + "

Shutting down ...

" ) % ( "
 - ".join(errorData) )) @@ -276,34 +278,44 @@ def main(sysArgs=None): nwApp.setWindowIcon(QIcon(CONFIG.appIcon)) nwApp.setOrganizationDomain(__domain__) + # We try to catch critical errors while setting up the main GUI + # by wrapping the main GUI in a try/except structure. This will + # not catch all exceptions for other parts of the application. + # For all other unhandled exceptions, we use a custom exception + # handler that pops a dialog box with the error message. + sys.excepthook = exceptionHandler + try: nwGUI = GuiMain() sys.exit(nwApp.exec_()) except Exception: - # novelWriter has crashed! - from traceback import print_tb, format_tb - eInfo = sys.exc_info() - logger.critical("%s: %s" % (eInfo[0].__name__, eInfo[1])) - print_tb(eInfo[2]) + from traceback import print_tb + from nw.error import formatHtmlErrMsg - del nwApp + exType, exValue, exTrace = sys.exc_info() - errApp = QApplication([]) - errMsg = QErrorMessage() - errMsg.setWindowTitle("Critical Error") - errMsg.setMinimumWidth(500) - errMsg.setMinimumHeight(300) - errMsg.showMessage(( - "

novelWriter has encountered a critical error!

" - "

%s:
%s

" - "

Traceback:
%s

" - "

Shutting down ...

" - ) % (eInfo[0].__name__, eInfo[1], "
".join(format_tb(eInfo[2])))) - errApp.exec_() + logger.critical("%s: %s" % (exType.__name__, str(exValue))) + print_tb(exTrace) - del eInfo + try: + del nwApp + + errApp = QApplication([]) + errMsg = QErrorMessage() + errMsg.setWindowTitle("Critical Error") + errMsg.resize(800, 400) + errMsg.showMessage(( + "

A critical error has been encountered

" + "%s" + "

Shutting down ...

" + ) % formatHtmlErrMsg(exType, exValue, exTrace)) + errApp.exec_() + + except Exception as e: + logger.critical("Could not create error message dialog.") + logger.critical(str(e)) sys.exit(1) diff --git a/nw/guimain.py b/nw/guimain.py index 6ea48c9b..12655f20 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -56,6 +56,7 @@ class GuiMain(QMainWindow): QMainWindow.__init__(self) logger.debug("Initialising GUI ...") + self.setObjectName("GuiMain") self.mainConf = nw.CONFIG # Some runtime info useful for debugging