From 4c025493b10ac8c720a5eed4653d1fd1659196f2 Mon Sep 17 00:00:00 2001 From: Michael Stephens Date: Thu, 1 Jul 2010 13:47:22 -0400 Subject: [PATCH] improve exceptions --- saucebrush/__init__.py | 19 +++++++++++++++---- saucebrush/tests/recipes.py | 10 +++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/saucebrush/__init__.py b/saucebrush/__init__.py index 7df97e4..4a9babd 100644 --- a/saucebrush/__init__.py +++ b/saucebrush/__init__.py @@ -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 diff --git a/saucebrush/tests/recipes.py b/saucebrush/tests/recipes.py index da8e6eb..288e6e7 100644 --- a/saucebrush/tests/recipes.py +++ b/saucebrush/tests/recipes.py @@ -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])