main: show error message for uncaught exceptions
parent
cbfcbf72ad
commit
dffdfa130b
|
|
@ -1,5 +1,8 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
import traceback
|
||||
|
||||
from PyQt5 import QtWidgets, QtCore
|
||||
from PyQt5.QtCore import pyqtSignal
|
||||
from fbs_runtime.application_context.PyQt5 import ApplicationContext
|
||||
|
||||
import sys
|
||||
|
|
@ -7,8 +10,48 @@ import sys
|
|||
from main_window import MainWindow
|
||||
from constants import WINDOW_WIDTH, WINDOW_HEIGHT
|
||||
|
||||
|
||||
# http://timlehr.com/python-exception-hooks-with-qt-message-box/
|
||||
def show_exception_box(log_msg):
|
||||
if QtWidgets.QApplication.instance() is not None:
|
||||
errorbox = QtWidgets.QMessageBox()
|
||||
errorbox.setText(log_msg)
|
||||
errorbox.exec_()
|
||||
|
||||
|
||||
class UncaughtHook(QtCore.QObject):
|
||||
_exception_caught = pyqtSignal(object)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(UncaughtHook, self).__init__(*args, **kwargs)
|
||||
|
||||
# this registers the exception_hook() function as hook with the Python interpreter
|
||||
sys._excepthook = sys.excepthook
|
||||
sys.excepthook = self.exception_hook
|
||||
|
||||
# connect signal to execute the message box function always on main thread
|
||||
self._exception_caught.connect(show_exception_box)
|
||||
|
||||
def exception_hook(self, exc_type, exc_value, exc_traceback):
|
||||
if issubclass(exc_type, KeyboardInterrupt):
|
||||
# ignore keyboard interrupt to support console applications
|
||||
sys.__excepthook__(exc_type, exc_value, exc_traceback)
|
||||
else:
|
||||
log_msg = '\n'.join([''.join(traceback.format_tb(exc_traceback)),
|
||||
'{0}: {1}'.format(exc_type.__name__, exc_value)])
|
||||
|
||||
# trigger message box show
|
||||
self._exception_caught.emit(log_msg)
|
||||
sys._excepthook(exc_type, exc_value, exc_traceback)
|
||||
|
||||
|
||||
# create a global instance of our class to register the hook
|
||||
qt_exception_hook = UncaughtHook()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
appctxt = ApplicationContext() # 1. Instantiate ApplicationContext
|
||||
qt_exception_hook = UncaughtHook()
|
||||
window = MainWindow()
|
||||
window.resize(WINDOW_WIDTH, WINDOW_HEIGHT)
|
||||
window.show()
|
||||
|
|
|
|||
Loading…
Reference in New Issue