Cleanup exceptions

The exceptions were not appropriate for a library.

Closes-Bug: 1469832
Change-Id: I823d9cddd63080654d51b6f9d04f0e7c2b657fc1
This commit is contained in:
Brant Knudson 2015-06-28 18:07:04 -05:00
parent 98537c8e0d
commit 773e7be008
7 changed files with 25 additions and 103 deletions

View File

@ -120,7 +120,7 @@ class ConnectionPool(queue.Queue):
try:
conn = self.get(timeout=self._connection_get_timeout)
except queue.Empty:
raise exception.UnexpectedError(
raise exception.QueueEmpty(
_('Unable to get a connection from pool id %(id)s after '
'%(seconds)s seconds.') %
{'id': id(self), 'seconds': self._connection_get_timeout})

View File

@ -229,17 +229,17 @@ class MongoApi(object):
self.hosts = arguments.pop('db_hosts', None)
if self.hosts is None:
msg = _('db_hosts value is required')
raise exception.ValidationError(message=msg)
raise exception.ConfigurationError(msg)
self.db_name = arguments.pop('db_name', None)
if self.db_name is None:
msg = _('database db_name is required')
raise exception.ValidationError(message=msg)
raise exception.ConfigurationError(msg)
self.cache_collection = arguments.pop('cache_collection', None)
if self.cache_collection is None:
msg = _('cache_collection name is required')
raise exception.ValidationError(message=msg)
raise exception.ConfigurationError(msg)
self.username = arguments.pop('username', None)
self.password = arguments.pop('password', None)
@ -250,7 +250,7 @@ class MongoApi(object):
self.w = int(self.w)
except ValueError:
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)
@ -258,7 +258,7 @@ class MongoApi(object):
if self.use_replica:
if arguments.get('replicaset_name') is None:
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.son_manipulator = arguments.pop('son_manipulator', None)
@ -273,7 +273,7 @@ class MongoApi(object):
self.ttl_seconds = int(self.ttl_seconds)
except ValueError:
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)
if self.conn_kwargs['ssl']:
@ -298,7 +298,7 @@ class MongoApi(object):
try:
import ssl
except ImportError:
raise exception.ValidationError(_('no ssl support available'))
raise exception.ConfigurationError(_('no ssl support available'))
req_type = req_type.upper()
try:
return {
@ -309,7 +309,7 @@ class MongoApi(object):
except KeyError:
msg = _('Invalid ssl_cert_reqs value of %s, must be one of '
'"NONE", "OPTIONAL", "REQUIRED"') % (req_type)
raise exception.ValidationError(message=msg)
raise exception.ConfigurationError(msg)
def _get_db(self):
# defer imports until backend is used
@ -498,7 +498,7 @@ class AbstractManipulator(object):
:returns: transformed SON object
"""
raise exception.NotImplemented() # pragma: no cover
raise NotImplementedError() # pragma: no cover
@abc.abstractmethod
def transform_outgoing(self, son, collection):
@ -509,7 +509,7 @@ class AbstractManipulator(object):
:returns: transformed SON object
"""
raise exception.NotImplemented() # pragma: no cover
raise NotImplementedError() # pragma: no cover
def will_copy(self):
"""Will this SON manipulator make a copy of the incoming document?

View File

@ -129,11 +129,11 @@ def configure_cache_region(region):
:param region: optional CacheRegion object, if not provided a new region
will be instantiated
:raises: exception.ValidationError
:raises: exception.ConfigurationError
:returns: dogpile.cache.CacheRegion
"""
if not isinstance(region, dogpile.cache.CacheRegion):
raise exception.ValidationError(
raise exception.ConfigurationError(
_('region not type dogpile.cache.CacheRegion'))
if not region.is_configured:

View File

@ -12,88 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging as log
from oslo_config import cfg
from oslo_utils import encodeutils
import six
from oslo_cache._i18n import _, _LW
class ConfigurationError(Exception):
"""Raised when the cache isn't configured correctly."""
CONF = cfg.CONF
LOG = log.getLogger(__name__)
# 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'
class QueueEmpty(Exception):
"""Raised when a connection cannot be acquired."""

View File

@ -318,7 +318,7 @@ class CacheRegionTest(BaseTestCase):
self.assertEqual(NO_VALUE, value)
def test_configure_non_region_object_raises_error(self):
self.assertRaises(exception.ValidationError,
self.assertRaises(exception.ConfigurationError,
cache.configure_cache_region,
"bogus")

View File

@ -297,28 +297,28 @@ class MongoCache(test_cache.BaseTestCase):
def test_missing_db_hosts(self):
self.arguments.pop('db_hosts')
region = dp_region.make_region()
self.assertRaises(exception.ValidationError, region.configure,
self.assertRaises(exception.ConfigurationError, region.configure,
'oslo_cache.mongo',
arguments=self.arguments)
def test_missing_db_name(self):
self.arguments.pop('db_name')
region = dp_region.make_region()
self.assertRaises(exception.ValidationError, region.configure,
self.assertRaises(exception.ConfigurationError, region.configure,
'oslo_cache.mongo',
arguments=self.arguments)
def test_missing_cache_collection_name(self):
self.arguments.pop('cache_collection')
region = dp_region.make_region()
self.assertRaises(exception.ValidationError, region.configure,
self.assertRaises(exception.ConfigurationError, region.configure,
'oslo_cache.mongo',
arguments=self.arguments)
def test_incorrect_write_concern(self):
self.arguments['w'] = 'one value'
region = dp_region.make_region()
self.assertRaises(exception.ValidationError, region.configure,
self.assertRaises(exception.ConfigurationError, region.configure,
'oslo_cache.mongo',
arguments=self.arguments)
@ -363,7 +363,7 @@ class MongoCache(test_cache.BaseTestCase):
def test_missing_replica_set_name(self):
self.arguments['use_replica'] = True
region = dp_region.make_region()
self.assertRaises(exception.ValidationError, region.configure,
self.assertRaises(exception.ConfigurationError, region.configure,
'oslo_cache.mongo',
arguments=self.arguments)
@ -377,7 +377,7 @@ class MongoCache(test_cache.BaseTestCase):
def test_incorrect_mongo_ttl_seconds(self):
self.arguments['mongo_ttl_seconds'] = 'sixty'
region = dp_region.make_region()
self.assertRaises(exception.ValidationError, region.configure,
self.assertRaises(exception.ConfigurationError, region.configure,
'oslo_cache.mongo',
arguments=self.arguments)

View File

@ -125,7 +125,7 @@ class TestConnectionPool(test_cache.BaseTestCase):
# Make sure we've consumed the only available connection from the pool
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
# after it is available.