CrashHandler.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.i18n import i18nCatalog
  8. catalog = i18nCatalog("cura")
  9. def show(exception_type, value, tb):
  10. debug_mode = False
  11. if QCoreApplication.instance():
  12. debug_mode = QCoreApplication.instance().getCommandLineOption("debug-mode", False)
  13. traceback.print_exception(exception_type, value, tb)
  14. if not debug_mode:
  15. return
  16. application = QCoreApplication.instance()
  17. if not application:
  18. sys.exit(1)
  19. dialog = QDialog()
  20. dialog.setWindowTitle(catalog.i18nc("@title:window", "Oops!"))
  21. layout = QVBoxLayout(dialog)
  22. label = QLabel(dialog)
  23. layout.addWidget(label)
  24. label.setText(catalog.i18nc("@label", "<p>An uncaught exception has occurred!</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>"))
  25. textarea = QTextEdit(dialog)
  26. layout.addWidget(textarea)
  27. try:
  28. from UM.Application import Application
  29. version = Application.getInstance().getVersion()
  30. except:
  31. version = "Unknown"
  32. trace = "".join(traceback.format_exception(exception_type, value, tb))
  33. crash_info = "Version: {0}\nPlatform: {1}\nQt: {2}\nPyQt: {3}\n\nException:\n{4}"
  34. crash_info = crash_info.format(version, platform.platform(), QT_VERSION_STR, PYQT_VERSION_STR, trace)
  35. textarea.setText(crash_info)
  36. buttons = QDialogButtonBox(QDialogButtonBox.Close, dialog)
  37. layout.addWidget(buttons)
  38. buttons.addButton(catalog.i18nc("@action:button", "Open Web Page"), QDialogButtonBox.HelpRole)
  39. buttons.rejected.connect(dialog.close)
  40. buttons.helpRequested.connect(lambda: webbrowser.open("http://github.com/Ultimaker/Cura/issues"))
  41. dialog.exec_()
  42. sys.exit(1)