CrashHandler.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import sys
  2. import platform
  3. import traceback
  4. import webbrowser
  5. from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, QCoreApplication
  6. from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QTextEdit
  7. from UM.Logger import Logger
  8. from UM.i18n import i18nCatalog
  9. catalog = i18nCatalog("cura")
  10. # List of exceptions that should be considered "fatal" and abort the program.
  11. # These are primarily some exception types that we simply cannot really recover from
  12. # (MemoryError and SystemError) and exceptions that indicate grave errors in the
  13. # code that cause the Python interpreter to fail (SyntaxError, ImportError).
  14. fatal_exception_types = [
  15. MemoryError,
  16. SyntaxError,
  17. ImportError,
  18. SystemError,
  19. ]
  20. def show(exception_type, value, tb):
  21. debug_mode = False
  22. if QCoreApplication.instance():
  23. debug_mode = QCoreApplication.instance().getCommandLineOption("debug-mode", False)
  24. Logger.log("c", "An uncaught exception has occurred!")
  25. for line in traceback.format_exception(exception_type, value, tb):
  26. for part in line.rstrip("\n").split("\n"):
  27. Logger.log("c", part)
  28. if not debug_mode and exception_type not in fatal_exception_types:
  29. return
  30. application = QCoreApplication.instance()
  31. if not application:
  32. sys.exit(1)
  33. dialog = QDialog()
  34. dialog.setWindowTitle(catalog.i18nc("@title:window", "Oops!"))
  35. layout = QVBoxLayout(dialog)
  36. label = QLabel(dialog)
  37. layout.addWidget(label)
  38. label.setText(catalog.i18nc("@label", "<p>A fatal exception has occurred that we could not recover from!</p><p>Please use the information below to post a bug report at <a href=\"http://github.com/Ultimaker/Cura/issues\">http://github.com/Ultimaker/Cura/issues</a></p>"))
  39. textarea = QTextEdit(dialog)
  40. layout.addWidget(textarea)
  41. try:
  42. from UM.Application import Application
  43. version = Application.getInstance().getVersion()
  44. except:
  45. version = "Unknown"
  46. trace = "".join(traceback.format_exception(exception_type, value, tb))
  47. crash_info = "Version: {0}\nPlatform: {1}\nQt: {2}\nPyQt: {3}\n\nException:\n{4}"
  48. crash_info = crash_info.format(version, platform.platform(), QT_VERSION_STR, PYQT_VERSION_STR, trace)
  49. textarea.setText(crash_info)
  50. buttons = QDialogButtonBox(QDialogButtonBox.Close, dialog)
  51. layout.addWidget(buttons)
  52. buttons.addButton(catalog.i18nc("@action:button", "Open Web Page"), QDialogButtonBox.HelpRole)
  53. buttons.rejected.connect(dialog.close)
  54. buttons.helpRequested.connect(lambda: webbrowser.open("http://github.com/Ultimaker/Cura/issues"))
  55. dialog.exec_()
  56. sys.exit(1)