Raise PluginNotFound exception when not found

A new exception class :class:`.exception.PluginNotFound` is now
raised when a particular cache plugin class cannot be located
either as a setuptools entrypoint or as a registered backend.
Previously, a plain ``Exception`` was thrown.  Pull request
courtesy Jamie Lennox.

Change-Id: Iceab2d613d2d802618dc0bed8ec2db694176c788
This commit is contained in:
Jamie Lennox 2016-07-05 10:20:59 +10:00 committed by Mike Bayer
parent eec8a9731b
commit e5508c0989
7 changed files with 46 additions and 5 deletions

6
docs/build/api.rst vendored
View File

@ -41,6 +41,12 @@ Backends
.. automodule:: dogpile.cache.backends.null
:members:
Exceptions
==========
.. automodule:: dogpile.cache.exception
:members:
Plugins
========

View File

@ -22,6 +22,15 @@ Changelog
ends up being used as a cache key do not create multiple values.
Pull request courtesy Paul Brown.
.. change::
:tags: bug
A new exception class :class:`.exception.PluginNotFound` is now
raised when a particular cache plugin class cannot be located
either as a setuptools entrypoint or as a registered backend.
Previously, a plain ``Exception`` was thrown. Pull request
courtesy Jamie Lennox.
.. changelog::
:version: 0.6.3
:released: Thu May 18, 2017

View File

@ -15,3 +15,11 @@ class RegionNotConfigured(DogpileCacheException):
class ValidationError(DogpileCacheException):
"""Error validating a value or option."""
class PluginNotFound(DogpileCacheException):
"""The specified plugin could not be found.
.. versionadded:: 0.6.4
"""

View File

@ -410,7 +410,13 @@ class CacheRegion(object):
"configured with backend: %s. "
"Specify replace_existing_backend=True to replace."
% self.backend)
backend_cls = _backend_loader.load(backend)
try:
backend_cls = _backend_loader.load(backend)
except PluginLoader.NotFound:
raise exception.PluginNotFound(
"Couldn't find cache plugin to load: %s" % backend)
if _config_argument_dict:
self.backend = backend_cls.from_config_dict(
_config_argument_dict,

View File

@ -1,4 +1,4 @@
from .nameregistry import NameRegistry # noqa
from .readwrite_lock import ReadWriteMutex # noqa
from .langhelpers import PluginLoader, memoized_property, \
coerce_string_conf, to_list, KeyReentrantMutex # noqa
coerce_string_conf, to_list, KeyReentrantMutex # noqa

View File

@ -39,9 +39,9 @@ class PluginLoader(object):
self.impls[name] = impl.load
return impl.load()
else:
raise Exception(
"Can't load plugin %s %s" %
(self.group, name))
raise self.NotFound(
"Can't load plugin %s %s" % (self.group, name)
)
def register(self, name, modulepath, objname):
def load():
@ -49,6 +49,9 @@ class PluginLoader(object):
return getattr(mod, objname)
self.impls[name] = load
class NotFound(Exception):
"""The specified plugin could not be found."""
class memoized_property(object):
"""A read-only @property that is only evaluated once."""

View File

@ -152,6 +152,15 @@ class RegionTest(TestCase):
)
eq_(reg.is_configured, False)
def test_invalid_backend(self):
reg = CacheRegion()
assert_raises_message(
exception.PluginNotFound,
"Couldn't find cache plugin to load: unknown",
reg.configure, 'unknown'
)
eq_(reg.is_configured, False)
def test_set_get_value(self):
reg = self._region()
reg.set("some key", "some value")