1. Mike Bayer
  2. dogpile.cache


Morgan Fainberg  committed 1721049

Add in dogpile.cache explicit exceptions

Replaced relevant cases of raising Exception with a dogpile.cache
explicit exception (for better handling). Also added in a simple
@property method to allow checking to see if a given CacheRegion
has been previously configured (a little more elegant than a
try/except block trying to access .backend).

  • Participants
  • Parent commits 3785f3b
  • Branches master

Comments (0)

Files changed (3)

File dogpile/cache/exception.py

View file
  • Ignore whitespace
+"""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."""

File dogpile/cache/region.py

View file
  • Ignore whitespace
 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())
-            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)
     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.

File tests/cache/test_region.py

View file
  • Ignore whitespace
 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()
-            Exception,
+            exception.ValidationError,
             "expiration_time is not a number or timedelta.",
             my_region.configure, 'mock', 'one hour'
         reg = CacheRegion()
-            Exception,
+            exception.RegionAlreadyConfigured,
             "This region is already configured",
             reg.configure, "mock"
+        eq_(reg.is_configured, True)
     def test_no_config(self):
         reg = CacheRegion()
-            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()