improve exceptions

This commit is contained in:
Michael Stephens 2010-07-01 13:47:22 -04:00
parent e8ce9948de
commit 4c025493b1
2 changed files with 22 additions and 7 deletions

View File

@ -5,6 +5,17 @@
import filters, emitters, sources, utils
class SaucebrushError(Exception):
pass
class OvercookedError(Exception):
"""
Exception for trying to operate on a Recipe that has been finished.
"""
pass
class Recipe(object):
def __init__(self, *filter_args, **kwargs):
@ -24,8 +35,8 @@ class Recipe(object):
elif hasattr(self.error_stream, '__iter__'):
self.error_stream = Recipe(*self.error_stream)
else:
raise ValueError('error_stream must be either a filter'
' or an iterable of filters')
raise SaucebrushError('error_stream must be either a filter'
' or an iterable of filters')
def reject_record(self, record, exception):
if self.error_stream:
@ -34,7 +45,7 @@ class Recipe(object):
def run(self, source):
if self.finished:
raise ValueError('run() called on finished recipe')
raise OvercookedError('run() called on finished recipe')
# connect datapath
data = source
@ -47,7 +58,7 @@ class Recipe(object):
def done(self):
if self.finished:
raise ValueError('done() called on finished recipe')
raise OvercookedError('done() called on finished recipe')
self.finished = True

View File

@ -1,6 +1,6 @@
import doctest
import unittest
from saucebrush import Recipe, run_recipe
from saucebrush import Recipe, run_recipe, SaucebrushError, OvercookedError
from saucebrush.filters import Filter
@ -28,6 +28,10 @@ class RecipeTestCase(unittest.TestCase):
self.assertEqual(saver.saved[0]['record'], {'a': 1})
self.assertEqual(saver.saved[1]['record'], {'b': 2})
# Must pass either a Recipe, a Filter or an iterable of Filters
# as the error_stream argument
self.assertRaises(SaucebrushError, Recipe, error_stream=5)
def test_run_recipe(self):
saver = Saver()
run_recipe([1, 2], saver)
@ -40,8 +44,8 @@ class RecipeTestCase(unittest.TestCase):
recipe.run([1])
recipe.done()
self.assertRaises(ValueError, recipe.run, [2])
self.assertRaises(ValueError, recipe.done)
self.assertRaises(OvercookedError, recipe.run, [2])
self.assertRaises(OvercookedError, recipe.done)
self.assertEqual(saver.saved, [1])