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 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): class Recipe(object):
def __init__(self, *filter_args, **kwargs): def __init__(self, *filter_args, **kwargs):
@ -24,7 +35,7 @@ class Recipe(object):
elif hasattr(self.error_stream, '__iter__'): elif hasattr(self.error_stream, '__iter__'):
self.error_stream = Recipe(*self.error_stream) self.error_stream = Recipe(*self.error_stream)
else: else:
raise ValueError('error_stream must be either a filter' raise SaucebrushError('error_stream must be either a filter'
' or an iterable of filters') ' or an iterable of filters')
def reject_record(self, record, exception): def reject_record(self, record, exception):
@ -34,7 +45,7 @@ class Recipe(object):
def run(self, source): def run(self, source):
if self.finished: if self.finished:
raise ValueError('run() called on finished recipe') raise OvercookedError('run() called on finished recipe')
# connect datapath # connect datapath
data = source data = source
@ -47,7 +58,7 @@ class Recipe(object):
def done(self): def done(self):
if self.finished: if self.finished:
raise ValueError('done() called on finished recipe') raise OvercookedError('done() called on finished recipe')
self.finished = True self.finished = True

View File

@ -1,6 +1,6 @@
import doctest import doctest
import unittest import unittest
from saucebrush import Recipe, run_recipe from saucebrush import Recipe, run_recipe, SaucebrushError, OvercookedError
from saucebrush.filters import Filter 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[0]['record'], {'a': 1})
self.assertEqual(saver.saved[1]['record'], {'b': 2}) 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): def test_run_recipe(self):
saver = Saver() saver = Saver()
run_recipe([1, 2], saver) run_recipe([1, 2], saver)
@ -40,8 +44,8 @@ class RecipeTestCase(unittest.TestCase):
recipe.run([1]) recipe.run([1])
recipe.done() recipe.done()
self.assertRaises(ValueError, recipe.run, [2]) self.assertRaises(OvercookedError, recipe.run, [2])
self.assertRaises(ValueError, recipe.done) self.assertRaises(OvercookedError, recipe.done)
self.assertEqual(saver.saved, [1]) self.assertEqual(saver.saved, [1])