| 1 |
|
|---|
| 2 |
"""A generic way to handle errors in GUI applications. |
|---|
| 3 |
|
|---|
| 4 |
This module also contains a ErrorHandlerAspect, which can be easely attached |
|---|
| 5 |
to a class' method and will raise the error dialog when the method exits with |
|---|
| 6 |
an exception. |
|---|
| 7 |
""" |
|---|
| 8 |
import gtk |
|---|
| 9 |
import sys |
|---|
| 10 |
import pdb |
|---|
| 11 |
|
|---|
| 12 |
from gaphor.i18n import _ |
|---|
| 13 |
from gaphor.misc.aspects import Aspect, weave_method |
|---|
| 14 |
|
|---|
| 15 |
def error_handler(message=None, exc_info=None): |
|---|
| 16 |
exc_type, exc_value, exc_traceback = exc_info or sys.exc_info() |
|---|
| 17 |
|
|---|
| 18 |
if not exc_type: |
|---|
| 19 |
return |
|---|
| 20 |
|
|---|
| 21 |
if not message: |
|---|
| 22 |
message = _('An error occured.') |
|---|
| 23 |
|
|---|
| 24 |
buttons = gtk.BUTTONS_OK |
|---|
| 25 |
message = '%s\n\nTechnical details:\n\t%s\n\t%s' % (message, exc_type, exc_value) |
|---|
| 26 |
|
|---|
| 27 |
if __debug__ and sys.stdin.isatty(): |
|---|
| 28 |
buttons = gtk.BUTTONS_YES_NO |
|---|
| 29 |
message += _('\n\nDo you want to debug?\n(Gaphor should have been started from the command line)') |
|---|
| 30 |
|
|---|
| 31 |
dialog = gtk.MessageDialog(None, |
|---|
| 32 |
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, |
|---|
| 33 |
gtk.MESSAGE_ERROR, |
|---|
| 34 |
buttons, message) |
|---|
| 35 |
answer = dialog.run() |
|---|
| 36 |
dialog.destroy() |
|---|
| 37 |
if answer == gtk.RESPONSE_YES: |
|---|
| 38 |
pdb.post_mortem(exc_traceback) |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
class ErrorHandlerAspect(Aspect): |
|---|
| 42 |
"""This aspect raises a Error dialog when the method wrapped by this |
|---|
| 43 |
aspect raises an exception. If the application is started from the |
|---|
| 44 |
command line, an option is presented to do post-mortem analysis of the |
|---|
| 45 |
error. |
|---|
| 46 |
""" |
|---|
| 47 |
|
|---|
| 48 |
def __init__(self, method, message=None): |
|---|
| 49 |
self.method = method |
|---|
| 50 |
self.message = message |
|---|
| 51 |
|
|---|
| 52 |
def after(self, retval, exc): |
|---|
| 53 |
if exc: |
|---|
| 54 |
error_handler(message=self.message) |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
if __name__ == '__main__': |
|---|
| 58 |
|
|---|
| 59 |
def func(x, y): |
|---|
| 60 |
if x == 0: |
|---|
| 61 |
raise Exception, 'Raised' |
|---|
| 62 |
func(x-1, y) |
|---|
| 63 |
|
|---|
| 64 |
try: |
|---|
| 65 |
func(5,5) |
|---|
| 66 |
except Exception: |
|---|
| 67 |
error_handler() |
|---|
| 68 |
|
|---|
| 69 |
print 'a' |
|---|
| 70 |
print 'a' |
|---|
| 71 |
print 'a' |
|---|