Set up global error handler and modified error handling for initial GUI build

This commit is contained in:
Veronica K. B. Olsen
2020-08-02 14:34:44 +02:00
parent e5cdb2e8ca
commit 95a5ee7b8a
2 changed files with 36 additions and 23 deletions
+35 -23
View File
@@ -34,6 +34,7 @@ from os import path, remove, rename
from PyQt5.QtGui import QIcon from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QErrorMessage from PyQt5.QtWidgets import QApplication, QErrorMessage
from nw.error import exceptionHandler
from nw.config import Config from nw.config import Config
__package__ = "nw" __package__ = "nw"
@@ -249,11 +250,12 @@ def main(sysArgs=None):
if errorData: if errorData:
errApp = QApplication([]) errApp = QApplication([])
errMsg = QErrorMessage() errMsg = QErrorMessage()
errMsg.setMinimumWidth(500) errMsg.resize(500, 300)
errMsg.setMinimumHeight(300)
errMsg.showMessage(( errMsg.showMessage((
"ERROR: novelWriter cannot start due to the following issues:<br><br>" "<h3>A critical error has been encountered</h3>"
"&nbsp;-&nbsp;%s<br><br>Exiting." "<p>novelWriter cannot start due to the following issues:<p>"
"<p>&nbsp;-&nbsp;%s</p>"
"<p>Shutting down ...</p>"
) % ( ) % (
"<br>&nbsp;-&nbsp;".join(errorData) "<br>&nbsp;-&nbsp;".join(errorData)
)) ))
@@ -276,34 +278,44 @@ def main(sysArgs=None):
nwApp.setWindowIcon(QIcon(CONFIG.appIcon)) nwApp.setWindowIcon(QIcon(CONFIG.appIcon))
nwApp.setOrganizationDomain(__domain__) 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: try:
nwGUI = GuiMain() nwGUI = GuiMain()
sys.exit(nwApp.exec_()) sys.exit(nwApp.exec_())
except Exception: except Exception:
# novelWriter has crashed!
from traceback import print_tb, format_tb
eInfo = sys.exc_info() from traceback import print_tb
logger.critical("%s: %s" % (eInfo[0].__name__, eInfo[1])) from nw.error import formatHtmlErrMsg
print_tb(eInfo[2])
del nwApp exType, exValue, exTrace = sys.exc_info()
errApp = QApplication([]) logger.critical("%s: %s" % (exType.__name__, str(exValue)))
errMsg = QErrorMessage() print_tb(exTrace)
errMsg.setWindowTitle("Critical Error")
errMsg.setMinimumWidth(500)
errMsg.setMinimumHeight(300)
errMsg.showMessage((
"<h3>novelWriter has encountered a critical error!</h3>"
"<p><b>%s:</b><br>%s</p>"
"<p><b>Traceback:</b><br>%s</p>"
"<p>Shutting down ...</p>"
) % (eInfo[0].__name__, eInfo[1], "<br>".join(format_tb(eInfo[2]))))
errApp.exec_()
del eInfo try:
del nwApp
errApp = QApplication([])
errMsg = QErrorMessage()
errMsg.setWindowTitle("Critical Error")
errMsg.resize(800, 400)
errMsg.showMessage((
"<h3>A critical error has been encountered</h3>"
"%s"
"<p>Shutting down ...</p>"
) % 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) sys.exit(1)
+1
View File
@@ -56,6 +56,7 @@ class GuiMain(QMainWindow):
QMainWindow.__init__(self) QMainWindow.__init__(self)
logger.debug("Initialising GUI ...") logger.debug("Initialising GUI ...")
self.setObjectName("GuiMain")
self.mainConf = nw.CONFIG self.mainConf = nw.CONFIG
# Some runtime info useful for debugging # Some runtime info useful for debugging