Mike Bayer avatar Mike Bayer committed 00dbd18 Merge

Merged in morgan_fainberg/dogpile.cache/exception_cleanup (pull request #4)

Add in dogpile.cache explicit exceptions

Comments (0)

Files changed (3)

dogpile/cache/exception.py

+"""Exception classes for dogpile.cache."""
+
+
+class DogpileCacheException(Exception):
+    """Base Exception for dogpile.cache exceptions to inherit from."""
+
+
+class RegionAlreadyConfigured(DogpileCacheException):
+    """CacheRegion instance is already configured."""
+
+
+class RegionNotConfigured(DogpileCacheException):
+    """CacheRegion instance has not been configured."""
+
+
+class ValidationError(DogpileCacheException):
+    """Error validating a value or option."""

dogpile/cache/region.py

 from __future__ import with_statement
 from dogpile.core import Lock, NeedRegenerationException
 from dogpile.core.nameregistry import NameRegistry
+from . import exception
 from .util import function_key_generator, PluginLoader, \
     memoized_property, coerce_string_conf, function_multi_key_generator
 from .api import NO_VALUE, CachedValue
          """
 
         if "backend" in self.__dict__:
-            raise Exception(
+            raise exception.RegionAlreadyConfigured(
                     "This region is already "
                     "configured with backend: %s"
                     % self.backend)
         elif isinstance(expiration_time, datetime.timedelta):
             self.expiration_time = int(expiration_time.total_seconds())
         else:
-            raise Exception('expiration_time is not a number or timedelta.')
+            raise exception.ValidationError(
+                'expiration_time is not a number or timedelta.')
 
         if self.key_mangler is None:
             self.key_mangler = self.backend.key_mangler
             proxy = proxy()
 
         if not issubclass(type(proxy), ProxyBackend):
-            raise Exception("Type %s is not a valid ProxyBackend"
-                    % type(proxy))
+            raise TypeError("Type %s is not a valid ProxyBackend"
+                            % type(proxy))
 
         self.backend = proxy.wrap(self.backend)
 
 
     @memoized_property
     def backend(self):
-        raise Exception("No backend is configured on this region.")
+        raise exception.RegionNotConfigured(
+            "No backend is configured on this region.")
+
+    @property
+    def is_configured(self):
+        """Public property that reports if the backend has been configured."""
+        return 'backend' in self.__dict__
 
     def get(self, key, expiration_time=None, ignore_expiration=False):
         """Return a value from the cache, based on the given key.

tests/cache/test_region.py

 import pprint
 from unittest import TestCase
 from dogpile.cache.api import CacheBackend, CachedValue, NO_VALUE
+from dogpile.cache import exception
 from dogpile.cache import make_region, register_backend, CacheRegion, util
 from dogpile.cache.proxy import ProxyBackend
 from . import eq_, is_, assert_raises_message, io, configparser
         my_region = make_region()
 
         assert_raises_message(
-            Exception,
+            exception.ValidationError,
             "expiration_time is not a number or timedelta.",
             my_region.configure, 'mock', 'one hour'
         )
         reg = CacheRegion()
         reg.configure("mock")
         assert_raises_message(
-            Exception,
+            exception.RegionAlreadyConfigured,
             "This region is already configured",
             reg.configure, "mock"
         )
+        eq_(reg.is_configured, True)
 
     def test_no_config(self):
         reg = CacheRegion()
         assert_raises_message(
-            Exception,
+            exception.RegionNotConfigured,
             "No backend is configured on this region.",
             getattr, reg, "backend"
         )
+        eq_(reg.is_configured, False)
 
     def test_set_get_value(self):
         reg = self._region()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.