-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
errorhandler.py
45 lines (35 loc) · 1.17 KB
/
errorhandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""A generic way to handle errors in GUI applications.
This module also contains a ErrorHandlerAspect, which can be easily attached
to a class' method and will raise the error dialog when the method exits with
an exception.
"""
from gi.repository import Gtk
import sys
import pdb
from gaphor.i18n import _
def error_handler(message=None, exc_info=None):
exc_type, exc_value, exc_traceback = exc_info or sys.exc_info()
if not exc_type:
return
if not message:
message = _("An error occurred.")
buttons = Gtk.ButtonsType.OK
message = (
f"{message}\n\n" + _("Technical details:") + f"\n\t{exc_type}\n\t{exc_value}"
)
if __debug__ and sys.stdin.isatty():
buttons = Gtk.ButtonsType.YES_NO
message += "\n\n" + _(
"Do you want to debug?\n(Gaphor should have been started from the command line)"
)
dialog = Gtk.MessageDialog(
None,
Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.ERROR,
buttons,
message,
)
answer = dialog.run()
dialog.destroy()
if answer == Gtk.ResponseType.YES:
pdb.post_mortem(exc_traceback)