From 6ae35a7a08eb202597e2f03b8337b7b7bea17b41 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Sun, 4 Apr 2010 23:10:44 +0000 Subject: [PATCH] * morituri/common/task.py: Wrap exceptions during tasks in a TaskException, storing the message, for improved error reporting later on. --- ChangeLog | 6 ++++++ morituri/common/task.py | 36 ++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index b8f0b29..179c4ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-04-05 Thomas Vander Stichele + + * morituri/common/task.py: + Wrap exceptions during tasks in a TaskException, storing the + message, for improved error reporting later on. + 2010-04-05 Thomas Vander Stichele * doc/Makefile.am: diff --git a/morituri/common/task.py b/morituri/common/task.py index 1541830..0dbef2f 100644 --- a/morituri/common/task.py +++ b/morituri/common/task.py @@ -26,6 +26,18 @@ import gobject from morituri.common import log +class TaskException(Exception): + """ + I wrap an exception that happened during task execution. + """ + + exception = None # original exception + + def __init__(self, exception, message=None): + self.exception = exception + self.exceptionMessage = message + self.args = (exception, message, ) + class Task(object, log.Loggable): """ I wrap a task in an asynchronous interface. @@ -94,6 +106,11 @@ class Task(object, log.Loggable): self._notifyListeners('described', description) self.description = description + def setException(self, exception): + self.exception = exception + self.exceptionMessage = log.getExceptionMessage(exception) + self.debug('set exception, %r' % self.exceptionMessage) + def addListener(self, listener): """ Add a listener for task status changes. @@ -214,10 +231,8 @@ class BaseMultiTask(Task, ITaskListener): task.addListener(self) task.start(self.runner) except Exception, e: - m = log.getExceptionMessage(e) - self.debug('Got exception during next: %r', m) - self.exception = e - self.exceptionMessage = m + self.setException(e) + self.debug('Got exception during next: %r', self.exceptionMessage) self.stop() return @@ -235,8 +250,10 @@ class BaseMultiTask(Task, ITaskListener): """ self.log('BaseMultiTask.stopped: task %r', task) if task.exception: - self.log('BaseMultiTask.stopped: exception %r', task.exception) + self.log('BaseMultiTask.stopped: exception %r', + task.exceptionMessage) self.exception = task.exception + self.exceptionMessage = task.exceptionMessage self.stop() return @@ -346,9 +363,10 @@ class SyncRunner(TaskRunner, ITaskListener): self.debug('done running task %r', task) if task.exception: + # catch the exception message # FIXME: this gave a traceback in the logging module self.debug('raising exception, %r', task.exceptionMessage) - raise task.exception + raise TaskException(task.exception, message=task.exceptionMessage) def _startWrap(self, task): # wrap task start such that we can report any exceptions and @@ -358,10 +376,8 @@ class SyncRunner(TaskRunner, ITaskListener): except Exception, e: # getExceptionMessage uses global exception state that doesn't # hang around, so store the message - m = log.getExceptionMessage(e) - self.debug('exception during start: %r', m) - task.exception = e - task.exceptionMessage = m + task.setException(e) + self.debug('exception during start: %r', task.exceptionMessage) self.stopped(task)