CrashHandler.py 3.4 KB

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