Line 1: |
Line 1: |
| ==Sugar should bring up an alert when an unhandled python exception occurs== | | ==Sugar should bring up an alert when an unhandled python exception occurs== |
| + | |
| + | ===Background Research=== |
| + | |
| + | Ideally, all exceptions are caught and this scenario should never happen. However, currently, users are left wondering why a certain operation could not be performed and there is nothing to direct them towards their logs which is full of tracebacks. |
| + | Therefore, it was required to print a notification alert whenever such a thing happens which stated “The operation requested could not be performed. Please check the logviewer activity for details” |
| + | However, there was some confusion between whether the ‘unhandled’ exceptions were being talked about as Daniel Drake suspected that they were referring to exceptions that are |
| + | not handled gracefully and instead fall back to a catch-all handler |
| + | which does not have the knowledge to act on them because technically the exceptions are not unhandled, if they were unhandled then sugar would crash. |
| + | Tomeu had expressed his desire to use the (Automated Bug Reporting Tool) -abrt functionality used by python processes for such issues. However, that would require some major changes and integration with the school server. |
| + | In my frequent conversations with Aleksey at IRC- #ubuntu-sugarteam regarding the issue, Aleksey expressed that the bug was undesirable to him and that he preferred something like a bug report. He also mentioned that any proper fix for the bug would be too invasive. |
| + | Initial Approach: |
| + | Since, any exception is always logged. Thus, if we could check whether logging.ERROR has been called, there ,an exception has occured .Therefore, if we introduce a flag which attains True value when an exception occurs, and by checking the value of the flag in a GUI file we can generate an alert/notification. For this purpose, I had tried to get a git diff of the sugar.logger and posted on sugar-devel for feedback. |
| + | However, this approach had major drawbacks as sugar uses standard python logging and any changes in that would be too invasive and inefficient. |
| + | |
| + | ===Method and Implementations=== |
| + | Whenever any uncaught exception is raised, the interpreter calls the sys.excepthook(type,value,traceback). After some discussions at #python, we learnt how this functionality could be used in this scenario and prepared a git diff of the file src/jarabe/journal/journalactivity.py |
| + | The following is the git diff. Please find the explanations as comments |
| + | diff --git a/journalactivity.py b/journalactivity.py |
| + | index 44cc018..36a2e2e 100644 |
| + | --- a/journalactivity.py |
| + | +++ b/journalactivity.py |
| + | @@ -358,8 +358,20 @@ class JournalActivity(Window): |
| + | self.show_main_view() |
| + | self.search_grab_focus() |
| + | |
| + | ''' Defining an Error Alert function in the Journal Activity Class which displays the message as |
| + | |
| + | Operation could not be performed |
| + | Please check the logviewer activity for details |
| + | and an OK button |
| + | ''' |
| + | |
| + | |
| + | + def uncaught_exception_alert(self): |
| + | + alert = ErrorAlert(title="Operation could not be performed", msg="Please check the logviewer activity for details ") |
| + | + alert.connect('response', self.__alert_response_cb) |
| + | + self.add_alert(alert) |
| + | + alert.show() |
| + | + |
| + | _journal = None |
| + | |
| + | ''' |
| + | Defining a function which logs the unhandled exception as well as calls the Error Alert in the Journal whenever any uncaught exception occurs |
| + | ''' |
| + | |
| + | +def _alert_excepthook(exc_type, exc_value, traceback): |
| + | + logging.exception('Unhandled Python exception: %s', repr((exc_type, exc_value, traceback))) |
| + | + _journal.uncaught_exception_alert() |
| + | + |
| + | ''' |
| + | When an exception is raised and uncaught, sys.excepthook is called with three arguments, the exception class, exception instance, and a traceback object.The function _alert_excepthook is called only when an unhandled exception occurs. |
| + | ''' |
| + | |
| + | +sys.excepthook = _alert_excepthook |
| + | + |
| + | def get_journal(): |
| + | global _journal |
| + | if _journal is None: |
| + | |
| + | |
| + | However, the above code caught the unhandled exceptions for only journal files and displayed an error alert in the journal except for journalactivity.py. I realized that whenever any unhandled exception occurs in the file the ‘Journal’ icon itself disappears and there is no other way to go the Journal. |
| + | |
| + | |
| + | ===Feedback=== |
| + | |
| + | 1. To catch all unhandled python exceptions in sugar, where exactly should we be looking forward to be the venue for adding the functionality? |
| + | |
| + | Adding it in journalactivity.py doesn't seem to serve all purposes. It has to be added somewhere which is being used all the time. |
| + | |
| + | 2. Which GUI feature should be used to add the alert> Adding an alert in Journal looks fine for only Journal related exceptions. |