Skip to content

Commit

Permalink
Fix exception wrapper to actually raise the correct exception class.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Jan 16, 2015
1 parent 9cd4724 commit eb5c096
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
60 changes: 60 additions & 0 deletions huey/tests/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

from huey import crontab
from huey import exceptions as huey_exceptions
from huey import Huey
from huey.api import QueueTask
from huey.backends.dummy import DummyDataStore
Expand Down Expand Up @@ -140,6 +141,65 @@ def test_error_raised(self):
# error
self.assertRaises(BampfException, huey.execute, task)

def test_internal_error(self):
"""
Verify that exceptions are wrapped with the special "huey"
exception classes.
"""
class SpecialException(Exception):
pass

class BrokenQueue(DummyQueue):
def read(self):
raise SpecialException('read error')

def write(self, data):
raise SpecialException('write error')

class BrokenDataStore(DummyDataStore):
def get(self, key):
raise SpecialException('get error')

def put(self, key, value):
raise SpecialException('put error')

class BrokenSchedule(DummySchedule):
def add(self, data, ts):
raise SpecialException('add error')

def read(self, ts):
raise SpecialException('read error')

task = AddTask()
huey = Huey(
BrokenQueue('q'),
BrokenDataStore('q'),
BrokenSchedule('q'))

self.assertRaises(
huey_exceptions.QueueWriteException,
huey.enqueue,
AddTask())
self.assertRaises(
huey_exceptions.QueueReadException,
huey.dequeue)
self.assertRaises(
huey_exceptions.DataStorePutException,
huey.revoke,
task)
self.assertRaises(
huey_exceptions.DataStoreGetException,
huey.restore,
task)
self.assertRaises(
huey_exceptions.ScheduleAddException,
huey.add_schedule,
task)
self.assertRaises(
huey_exceptions.ScheduleReadException,
huey.read_schedule,
1)

def test_dequeueing(self):
res = huey.dequeue() # no error raised if queue is empty
self.assertEqual(res, None)
Expand Down
4 changes: 2 additions & 2 deletions huey/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def load_class(s):
mod = sys.modules[path]
return getattr(mod, klass)

def wrap_exception(exc_class):
def wrap_exception(new_exc_class):
exc_class, exc, tb = sys.exc_info()
raise exc_class(exc.message)
raise new_exc_class(exc.message)

def local_to_utc(dt):
return datetime.datetime(*time.gmtime(time.mktime(dt.timetuple()))[:6])

0 comments on commit eb5c096

Please sign in to comment.