CrashHandler.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import sys
  2. import platform
  3. import traceback
  4. import webbrowser
  5. import urllib
  6. from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, Qt, QCoreApplication
  7. from PyQt5.QtGui import QPixmap
  8. from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QHBoxLayout, QVBoxLayout, QLabel, QTextEdit
  9. from UM.Logger import Logger
  10. from UM.i18n import i18nCatalog
  11. catalog = i18nCatalog("cura")
  12. # List of exceptions that should be considered "fatal" and abort the program.
  13. # These are primarily some exception types that we simply cannot really recover from
  14. # (MemoryError and SystemError) and exceptions that indicate grave errors in the
  15. # code that cause the Python interpreter to fail (SyntaxError, ImportError).
  16. fatal_exception_types = [
  17. MemoryError,
  18. SyntaxError,
  19. ImportError,
  20. SystemError,
  21. ]
  22. def show(exception_type, value, tb):
  23. debug_mode = True
  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.setMinimumWidth(640)
  35. dialog.setMinimumHeight(640)
  36. dialog.setWindowTitle(catalog.i18nc("@title:window", "Oops!"))
  37. layout = QVBoxLayout(dialog)
  38. label = QLabel(dialog)
  39. pixmap = QPixmap()
  40. try:
  41. data = urllib.request.urlopen("http://www.randomkittengenerator.com/cats/rotator.php").read()
  42. pixmap.loadFromData(data)
  43. except:
  44. try:
  45. from UM.Resources import Resources
  46. path = Resources.getPath(Resources.Images, "kitten.jpg")
  47. pixmap.load(path)
  48. except:
  49. pass
  50. pixmap = pixmap.scaled(150, 150)
  51. label.setPixmap(pixmap)
  52. label.setAlignment(Qt.AlignCenter)
  53. layout.addWidget(label)
  54. label = QLabel(dialog)
  55. layout.addWidget(label)
  56. #label.setScaledContents(True)
  57. label.setText(catalog.i18nc("@label", """<p>A fatal exception has occurred that we could not recover from!</p>
  58. <p>We hope this picture of a kitten helps you recover from the shock.</p>
  59. <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>
  60. """))
  61. textarea = QTextEdit(dialog)
  62. layout.addWidget(textarea)
  63. try:
  64. from UM.Application import Application
  65. version = Application.getInstance().getVersion()
  66. except:
  67. version = "Unknown"
  68. trace = "".join(traceback.format_exception(exception_type, value, tb))
  69. crash_info = "Version: {0}\nPlatform: {1}\nQt: {2}\nPyQt: {3}\n\nException:\n{4}"
  70. crash_info = crash_info.format(version, platform.platform(), QT_VERSION_STR, PYQT_VERSION_STR, trace)
  71. textarea.setText(crash_info)
  72. buttons = QDialogButtonBox(QDialogButtonBox.Close, dialog)
  73. layout.addWidget(buttons)
  74. buttons.addButton(catalog.i18nc("@action:button", "Open Web Page"), QDialogButtonBox.HelpRole)
  75. buttons.rejected.connect(dialog.close)
  76. buttons.helpRequested.connect(lambda: webbrowser.open("http://github.com/Ultimaker/Cura/issues"))
  77. dialog.exec_()
  78. sys.exit(1)