Skip to content

Commit

Permalink
Add API hook for enriching the Error result metadata.
Browse files Browse the repository at this point in the history
Fixes #590
  • Loading branch information
coleifer committed Mar 14, 2021
1 parent 5b65ff3 commit a9e2b51
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
26 changes: 15 additions & 11 deletions huey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,8 @@ def _execute(self, task, timestamp):

if self.results and not isinstance(task, PeriodicTask):
if exception is not None:
try:
tb = traceback.format_exc()
except AttributeError: # Seems to only happen on 3.4.
tb = '- unable to resolve traceback on Python 3.4 -'

self.put_result(task.id, Error({
'error': repr(exception),
'retries': task.retries,
'traceback': tb,
'task_id': task.id,
}))
error = self.build_error_result(task, exception)
self.put_result(task.id, error)
elif task_value is not None or self.store_none:
self.put_result(task.id, task_value)

Expand Down Expand Up @@ -456,6 +447,19 @@ def _run_post_execute(self, task, task_value, exception):
logger.exception('Unhandled exception calling post-execute '
'hook %s for %s.', name, task)

def build_error_result(self, task, exception):
try:
tb = traceback.format_exc()
except AttributeError: # Seems to only happen on 3.4.
tb = '- unable to resolve traceback on Python 3.4 -'

return Error({
'error': repr(exception),
'retries': task.retries,
'traceback': tb,
'task_id': task.id,
})

def _task_key(self, task_class, key):
return ':'.join((key, self._registry.task_to_string(task_class)))

Expand Down
30 changes: 30 additions & 0 deletions huey/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,3 +1286,33 @@ def ptask():

self.assertEqual(inspect.getdoc(add), 'Adds two numbers.')
self.assertEqual(inspect.getdoc(ptask), 'Sample periodic task.')


class TestCustomErrorMetadata(BaseTestCase):
def get_huey(self):
class CustomError(MemoryHuey):
def build_error_result(self, task, exc):
err = super(CustomError, self).build_error_result(task, exc)
err.metadata['name'] = task.name
err.metadata['args'] = task.args
return err
return CustomError(utc=False)

def test_custom_error(self):
@self.huey.task()
def task_e(n):
raise TestError('uh-oh')

re = task_e(0)
self.assertTrue(self.execute_next() is None)
self.assertEqual(self.huey.result_count(), 1) # Error result present.

# Resolves the result handle, which re-raises the error.
err = self.trap_exception(re)
self.assertEqual(err.metadata['error'], 'TestError(uh-oh)')
self.assertEqual(err.metadata['retries'], 0)
self.assertEqual(err.metadata['name'], 'task_e')
self.assertEqual(err.metadata['args'], (0,))

self.assertEqual(self.huey.result_count(), 0)
self.assertEqual(len(self.huey), 0)

1 comment on commit a9e2b51

@DjMoren
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Please sign in to comment.