Cleanup exceptions
The exceptions were not appropriate for a library. Closes-Bug: 1469832 Change-Id: I823d9cddd63080654d51b6f9d04f0e7c2b657fc1
This commit is contained in:
parent
98537c8e0d
commit
773e7be008
@ -120,7 +120,7 @@ class ConnectionPool(queue.Queue):
|
|||||||
try:
|
try:
|
||||||
conn = self.get(timeout=self._connection_get_timeout)
|
conn = self.get(timeout=self._connection_get_timeout)
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
raise exception.UnexpectedError(
|
raise exception.QueueEmpty(
|
||||||
_('Unable to get a connection from pool id %(id)s after '
|
_('Unable to get a connection from pool id %(id)s after '
|
||||||
'%(seconds)s seconds.') %
|
'%(seconds)s seconds.') %
|
||||||
{'id': id(self), 'seconds': self._connection_get_timeout})
|
{'id': id(self), 'seconds': self._connection_get_timeout})
|
||||||
|
@ -229,17 +229,17 @@ class MongoApi(object):
|
|||||||
self.hosts = arguments.pop('db_hosts', None)
|
self.hosts = arguments.pop('db_hosts', None)
|
||||||
if self.hosts is None:
|
if self.hosts is None:
|
||||||
msg = _('db_hosts value is required')
|
msg = _('db_hosts value is required')
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
|
|
||||||
self.db_name = arguments.pop('db_name', None)
|
self.db_name = arguments.pop('db_name', None)
|
||||||
if self.db_name is None:
|
if self.db_name is None:
|
||||||
msg = _('database db_name is required')
|
msg = _('database db_name is required')
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
|
|
||||||
self.cache_collection = arguments.pop('cache_collection', None)
|
self.cache_collection = arguments.pop('cache_collection', None)
|
||||||
if self.cache_collection is None:
|
if self.cache_collection is None:
|
||||||
msg = _('cache_collection name is required')
|
msg = _('cache_collection name is required')
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
|
|
||||||
self.username = arguments.pop('username', None)
|
self.username = arguments.pop('username', None)
|
||||||
self.password = arguments.pop('password', None)
|
self.password = arguments.pop('password', None)
|
||||||
@ -250,7 +250,7 @@ class MongoApi(object):
|
|||||||
self.w = int(self.w)
|
self.w = int(self.w)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
msg = _('integer value expected for w (write concern attribute)')
|
msg = _('integer value expected for w (write concern attribute)')
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
|
|
||||||
self.read_preference = arguments.pop('read_preference', None)
|
self.read_preference = arguments.pop('read_preference', None)
|
||||||
|
|
||||||
@ -258,7 +258,7 @@ class MongoApi(object):
|
|||||||
if self.use_replica:
|
if self.use_replica:
|
||||||
if arguments.get('replicaset_name') is None:
|
if arguments.get('replicaset_name') is None:
|
||||||
msg = _('replicaset_name required when use_replica is True')
|
msg = _('replicaset_name required when use_replica is True')
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
self.replicaset_name = arguments.get('replicaset_name')
|
self.replicaset_name = arguments.get('replicaset_name')
|
||||||
|
|
||||||
self.son_manipulator = arguments.pop('son_manipulator', None)
|
self.son_manipulator = arguments.pop('son_manipulator', None)
|
||||||
@ -273,7 +273,7 @@ class MongoApi(object):
|
|||||||
self.ttl_seconds = int(self.ttl_seconds)
|
self.ttl_seconds = int(self.ttl_seconds)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
msg = _('integer value expected for mongo_ttl_seconds')
|
msg = _('integer value expected for mongo_ttl_seconds')
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
|
|
||||||
self.conn_kwargs['ssl'] = arguments.pop('ssl', False)
|
self.conn_kwargs['ssl'] = arguments.pop('ssl', False)
|
||||||
if self.conn_kwargs['ssl']:
|
if self.conn_kwargs['ssl']:
|
||||||
@ -298,7 +298,7 @@ class MongoApi(object):
|
|||||||
try:
|
try:
|
||||||
import ssl
|
import ssl
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise exception.ValidationError(_('no ssl support available'))
|
raise exception.ConfigurationError(_('no ssl support available'))
|
||||||
req_type = req_type.upper()
|
req_type = req_type.upper()
|
||||||
try:
|
try:
|
||||||
return {
|
return {
|
||||||
@ -309,7 +309,7 @@ class MongoApi(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
msg = _('Invalid ssl_cert_reqs value of %s, must be one of '
|
msg = _('Invalid ssl_cert_reqs value of %s, must be one of '
|
||||||
'"NONE", "OPTIONAL", "REQUIRED"') % (req_type)
|
'"NONE", "OPTIONAL", "REQUIRED"') % (req_type)
|
||||||
raise exception.ValidationError(message=msg)
|
raise exception.ConfigurationError(msg)
|
||||||
|
|
||||||
def _get_db(self):
|
def _get_db(self):
|
||||||
# defer imports until backend is used
|
# defer imports until backend is used
|
||||||
@ -498,7 +498,7 @@ class AbstractManipulator(object):
|
|||||||
:returns: transformed SON object
|
:returns: transformed SON object
|
||||||
|
|
||||||
"""
|
"""
|
||||||
raise exception.NotImplemented() # pragma: no cover
|
raise NotImplementedError() # pragma: no cover
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def transform_outgoing(self, son, collection):
|
def transform_outgoing(self, son, collection):
|
||||||
@ -509,7 +509,7 @@ class AbstractManipulator(object):
|
|||||||
|
|
||||||
:returns: transformed SON object
|
:returns: transformed SON object
|
||||||
"""
|
"""
|
||||||
raise exception.NotImplemented() # pragma: no cover
|
raise NotImplementedError() # pragma: no cover
|
||||||
|
|
||||||
def will_copy(self):
|
def will_copy(self):
|
||||||
"""Will this SON manipulator make a copy of the incoming document?
|
"""Will this SON manipulator make a copy of the incoming document?
|
||||||
|
@ -129,11 +129,11 @@ def configure_cache_region(region):
|
|||||||
|
|
||||||
:param region: optional CacheRegion object, if not provided a new region
|
:param region: optional CacheRegion object, if not provided a new region
|
||||||
will be instantiated
|
will be instantiated
|
||||||
:raises: exception.ValidationError
|
:raises: exception.ConfigurationError
|
||||||
:returns: dogpile.cache.CacheRegion
|
:returns: dogpile.cache.CacheRegion
|
||||||
"""
|
"""
|
||||||
if not isinstance(region, dogpile.cache.CacheRegion):
|
if not isinstance(region, dogpile.cache.CacheRegion):
|
||||||
raise exception.ValidationError(
|
raise exception.ConfigurationError(
|
||||||
_('region not type dogpile.cache.CacheRegion'))
|
_('region not type dogpile.cache.CacheRegion'))
|
||||||
|
|
||||||
if not region.is_configured:
|
if not region.is_configured:
|
||||||
|
@ -12,88 +12,10 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import logging as log
|
|
||||||
|
|
||||||
from oslo_config import cfg
|
class ConfigurationError(Exception):
|
||||||
from oslo_utils import encodeutils
|
"""Raised when the cache isn't configured correctly."""
|
||||||
import six
|
|
||||||
|
|
||||||
from oslo_cache._i18n import _, _LW
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
class QueueEmpty(Exception):
|
||||||
LOG = log.getLogger(__name__)
|
"""Raised when a connection cannot be acquired."""
|
||||||
|
|
||||||
# Tests use this to make exception message format errors fatal
|
|
||||||
_FATAL_EXCEPTION_FORMAT_ERRORS = False
|
|
||||||
|
|
||||||
|
|
||||||
class Error(Exception):
|
|
||||||
"""Base error class.
|
|
||||||
|
|
||||||
Child classes should define an HTTP status code, title, and a
|
|
||||||
message_format.
|
|
||||||
|
|
||||||
"""
|
|
||||||
code = None
|
|
||||||
title = None
|
|
||||||
message_format = None
|
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
|
||||||
try:
|
|
||||||
message = self._build_message(message, **kwargs)
|
|
||||||
except KeyError:
|
|
||||||
# if you see this warning in your logs, please raise a bug report
|
|
||||||
if _FATAL_EXCEPTION_FORMAT_ERRORS:
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
LOG.warning(_LW('missing exception kwargs (programmer error)'))
|
|
||||||
message = self.message_format
|
|
||||||
|
|
||||||
super(Error, self).__init__(message)
|
|
||||||
|
|
||||||
def _build_message(self, message, **kwargs):
|
|
||||||
"""Builds and returns an exception message.
|
|
||||||
|
|
||||||
:raises: KeyError given insufficient kwargs
|
|
||||||
|
|
||||||
"""
|
|
||||||
if not message:
|
|
||||||
try:
|
|
||||||
message = self.message_format % kwargs
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
try:
|
|
||||||
kwargs = {k: encodeutils.safe_decode(v)
|
|
||||||
for k, v in six.iteritems(kwargs)}
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# NOTE(jamielennox): This is the complete failure case
|
|
||||||
# at least by showing the template we have some idea
|
|
||||||
# of where the error is coming from
|
|
||||||
message = self.message_format
|
|
||||||
else:
|
|
||||||
message = self.message_format % kwargs
|
|
||||||
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
class ValidationError(Error):
|
|
||||||
message_format = _("Expecting to find %(attribute)s in %(target)s -"
|
|
||||||
" the server could not comply with the request"
|
|
||||||
" since it is either malformed or otherwise"
|
|
||||||
" incorrect. The client is assumed to be in error.")
|
|
||||||
code = 400
|
|
||||||
title = 'Bad Request'
|
|
||||||
|
|
||||||
|
|
||||||
class NotImplemented(Error):
|
|
||||||
message_format = _("The action you have requested has not"
|
|
||||||
" been implemented.")
|
|
||||||
code = 501
|
|
||||||
title = 'Not Implemented'
|
|
||||||
|
|
||||||
|
|
||||||
class UnexpectedError(Error):
|
|
||||||
message_format = _("An unexpected error prevented the server from "
|
|
||||||
"fulfilling your request.")
|
|
||||||
code = 500
|
|
||||||
title = 'Internal Server Error'
|
|
||||||
|
@ -318,7 +318,7 @@ class CacheRegionTest(BaseTestCase):
|
|||||||
self.assertEqual(NO_VALUE, value)
|
self.assertEqual(NO_VALUE, value)
|
||||||
|
|
||||||
def test_configure_non_region_object_raises_error(self):
|
def test_configure_non_region_object_raises_error(self):
|
||||||
self.assertRaises(exception.ValidationError,
|
self.assertRaises(exception.ConfigurationError,
|
||||||
cache.configure_cache_region,
|
cache.configure_cache_region,
|
||||||
"bogus")
|
"bogus")
|
||||||
|
|
||||||
|
@ -297,28 +297,28 @@ class MongoCache(test_cache.BaseTestCase):
|
|||||||
def test_missing_db_hosts(self):
|
def test_missing_db_hosts(self):
|
||||||
self.arguments.pop('db_hosts')
|
self.arguments.pop('db_hosts')
|
||||||
region = dp_region.make_region()
|
region = dp_region.make_region()
|
||||||
self.assertRaises(exception.ValidationError, region.configure,
|
self.assertRaises(exception.ConfigurationError, region.configure,
|
||||||
'oslo_cache.mongo',
|
'oslo_cache.mongo',
|
||||||
arguments=self.arguments)
|
arguments=self.arguments)
|
||||||
|
|
||||||
def test_missing_db_name(self):
|
def test_missing_db_name(self):
|
||||||
self.arguments.pop('db_name')
|
self.arguments.pop('db_name')
|
||||||
region = dp_region.make_region()
|
region = dp_region.make_region()
|
||||||
self.assertRaises(exception.ValidationError, region.configure,
|
self.assertRaises(exception.ConfigurationError, region.configure,
|
||||||
'oslo_cache.mongo',
|
'oslo_cache.mongo',
|
||||||
arguments=self.arguments)
|
arguments=self.arguments)
|
||||||
|
|
||||||
def test_missing_cache_collection_name(self):
|
def test_missing_cache_collection_name(self):
|
||||||
self.arguments.pop('cache_collection')
|
self.arguments.pop('cache_collection')
|
||||||
region = dp_region.make_region()
|
region = dp_region.make_region()
|
||||||
self.assertRaises(exception.ValidationError, region.configure,
|
self.assertRaises(exception.ConfigurationError, region.configure,
|
||||||
'oslo_cache.mongo',
|
'oslo_cache.mongo',
|
||||||
arguments=self.arguments)
|
arguments=self.arguments)
|
||||||
|
|
||||||
def test_incorrect_write_concern(self):
|
def test_incorrect_write_concern(self):
|
||||||
self.arguments['w'] = 'one value'
|
self.arguments['w'] = 'one value'
|
||||||
region = dp_region.make_region()
|
region = dp_region.make_region()
|
||||||
self.assertRaises(exception.ValidationError, region.configure,
|
self.assertRaises(exception.ConfigurationError, region.configure,
|
||||||
'oslo_cache.mongo',
|
'oslo_cache.mongo',
|
||||||
arguments=self.arguments)
|
arguments=self.arguments)
|
||||||
|
|
||||||
@ -363,7 +363,7 @@ class MongoCache(test_cache.BaseTestCase):
|
|||||||
def test_missing_replica_set_name(self):
|
def test_missing_replica_set_name(self):
|
||||||
self.arguments['use_replica'] = True
|
self.arguments['use_replica'] = True
|
||||||
region = dp_region.make_region()
|
region = dp_region.make_region()
|
||||||
self.assertRaises(exception.ValidationError, region.configure,
|
self.assertRaises(exception.ConfigurationError, region.configure,
|
||||||
'oslo_cache.mongo',
|
'oslo_cache.mongo',
|
||||||
arguments=self.arguments)
|
arguments=self.arguments)
|
||||||
|
|
||||||
@ -377,7 +377,7 @@ class MongoCache(test_cache.BaseTestCase):
|
|||||||
def test_incorrect_mongo_ttl_seconds(self):
|
def test_incorrect_mongo_ttl_seconds(self):
|
||||||
self.arguments['mongo_ttl_seconds'] = 'sixty'
|
self.arguments['mongo_ttl_seconds'] = 'sixty'
|
||||||
region = dp_region.make_region()
|
region = dp_region.make_region()
|
||||||
self.assertRaises(exception.ValidationError, region.configure,
|
self.assertRaises(exception.ConfigurationError, region.configure,
|
||||||
'oslo_cache.mongo',
|
'oslo_cache.mongo',
|
||||||
arguments=self.arguments)
|
arguments=self.arguments)
|
||||||
|
|
||||||
|
@ -125,7 +125,7 @@ class TestConnectionPool(test_cache.BaseTestCase):
|
|||||||
# Make sure we've consumed the only available connection from the pool
|
# Make sure we've consumed the only available connection from the pool
|
||||||
conn = connection_pool.get_nowait()
|
conn = connection_pool.get_nowait()
|
||||||
|
|
||||||
self.assertRaises(exception.UnexpectedError, _acquire_connection)
|
self.assertRaises(exception.QueueEmpty, _acquire_connection)
|
||||||
|
|
||||||
# Put the connection back and ensure we can acquire the connection
|
# Put the connection back and ensure we can acquire the connection
|
||||||
# after it is available.
|
# after it is available.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user