Browse Source

Handle all uncaught exceptions through CrashHandler and gracefully fail if we have no QCoreApplication

Arjen Hiemstra 9 years ago
parent
commit
803b4fde8d
2 changed files with 18 additions and 9 deletions
  1. 9 3
      cura/CrashHandler.py
  2. 9 6
      cura_app.py

+ 9 - 3
cura/CrashHandler.py

@@ -3,10 +3,15 @@ import platform
 import traceback
 import webbrowser
 
-from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR
+from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, QCoreApplication
 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QTextEdit
 
-def show():
+def show(type, value, tb):
+    application = QCoreApplication.instance()
+    if not application:
+        traceback.print_exception(type, value, tb)
+        exit(1)
+
     dialog = QDialog()
     dialog.setWindowTitle("Oops!")
 
@@ -25,7 +30,7 @@ def show():
     except:
         version = "Unknown"
 
-    trace = "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
+    trace = "".join(traceback.format_exception(type, value, tb))
 
     crash_info = "Version: {0}\nPlatform: {1}\nQt: {2}\nPyQt: {3}\n\nException:\n{4}"
     crash_info = crash_info.format(version, platform.platform(), QT_VERSION_STR, PYQT_VERSION_STR, trace)
@@ -39,3 +44,4 @@ def show():
     buttons.helpRequested.connect(lambda: webbrowser.open("http://github.com/Ultimaker/Cura/issues"))
 
     dialog.exec_()
+    exit(1)

+ 9 - 6
cura_app.py

@@ -3,12 +3,15 @@
 # Copyright (c) 2015 Ultimaker B.V.
 # Cura is released under the terms of the AGPLv3 or higher.
 
-try:
-    import cura.CuraApplication
+import sys
 
-    app = cura.CuraApplication.CuraApplication.getInstance()
-    app.run()
-except Exception as e:
+def exceptHook(type, value, traceback):
     import cura.CrashHandler
-    cura.CrashHandler.show()
+    cura.CrashHandler.show(type, value, traceback)
 
+sys.excepthook = exceptHook
+
+import cura.CuraApplication
+
+app = cura.CuraApplication.CuraApplication.getInstance()
+app.run()