From afb12adab08d38e0878611722846b85d2ee95fde Mon Sep 17 00:00:00 2001 From: tengqm Date: Fri, 20 Mar 2015 12:30:10 +0800 Subject: [PATCH] First round cleansing of exceptions There are too many exception types defined. This patch cleanse some of them. Following patches upcoming will try combine some exceptions. Change-Id: I17608a9c01677ec25a7905242944cf14ad1d0dda --- senlin/api/middleware/fault.py | 7 -- senlin/common/exception.py | 101 ---------------------------- senlin/db/sqlalchemy/api.py | 5 +- senlin/tests/db/test_cluster_api.py | 2 +- senlin/tests/test_wsgi.py | 4 +- 5 files changed, 5 insertions(+), 114 deletions(-) diff --git a/senlin/api/middleware/fault.py b/senlin/api/middleware/fault.py index 5c8313e03..0bb37c216 100644 --- a/senlin/api/middleware/fault.py +++ b/senlin/api/middleware/fault.py @@ -49,14 +49,10 @@ class FaultWrapper(wsgi.Middleware): 'ActionInProgress': webob.exc.HTTPConflict, 'ActionNotFound': webob.exc.HTTPNotFound, 'AttributeError': webob.exc.HTTPBadRequest, - 'ClusterActionNotSupported': webob.exc.HTTPBadRequest, - 'ClusterExists': webob.exc.HTTPConflict, 'ClusterNotFound': webob.exc.HTTPNotFound, 'EventNotFound': webob.exc.HTTPNotFound, 'Forbidden': webob.exc.HTTPForbidden, - 'Invalid': webob.exc.HTTPBadRequest, 'InvalidParameter': webob.exc.HTTPBadRequest, - 'InvalidTenant': webob.exc.HTTPForbidden, 'InvalidSchemaError': webob.exc.HTTPBadRequest, 'NodeNotFound': webob.exc.HTTPNotFound, 'NotFound': webob.exc.HTTPNotFound, @@ -69,11 +65,8 @@ class FaultWrapper(wsgi.Middleware): 'ProjectNotMatch': webob.exc.HTTPBadRequest, 'ProfileTypeNotMatch': webob.exc.HTTPBadRequest, 'RequestLimitExceeded': webob.exc.HTTPBadRequest, - 'RevertFailed': webob.exc.HTTPInternalServerError, 'SenlinBadRequest': webob.exc.HTTPBadRequest, - 'ServerBuildFailed': webob.exc.HTTPInternalServerError, 'SpecValidationFailed': webob.exc.HTTPBadRequest, - 'StopActionFailed': webob.exc.HTTPInternalServerError, 'ValueError': webob.exc.HTTPBadRequest, } diff --git a/senlin/common/exception.py b/senlin/common/exception.py index c85355408..89f2df8bb 100644 --- a/senlin/common/exception.py +++ b/senlin/common/exception.py @@ -23,7 +23,6 @@ import sys from oslo_log import log as logging import six -from six.moves.urllib import parse as urlparse from senlin.common.i18n import _ from senlin.common.i18n import _LE @@ -32,20 +31,6 @@ _FATAL_EXCEPTION_FORMAT_ERRORS = False LOG = logging.getLogger(__name__) -class RedirectException(Exception): - def __init__(self, url): - self.url = urlparse.urlparse(url) - - -class KeystoneError(Exception): - def __init__(self, code, message): - self.code = code - self.message = message - - def __str__(self): - return "Code: %s, message: %s" % (self.code, self.message) - - def wrap_exception(notifier=None, publisher_id=None, event_type=None, level=None): '''Decorator that wraps a method to catch any exceptions. @@ -127,27 +112,6 @@ class SenlinException(Exception): return self.__class__(**self.kwargs) -class MissingCredentialError(SenlinException): - msg_fmt = _("Missing required credential: %(required)s") - - -class BadAuthStrategy(SenlinException): - msg_fmt = _("Incorrect auth strategy, expected \"%(expected)s\" but " - "received \"%(received)s\"") - - -class AuthBadRequest(SenlinException): - msg_fmt = _("Connect error/bad request to Auth service at URL %(url)s.") - - -class AuthUrlNotFound(SenlinException): - msg_fmt = _("Auth service at URL %(url)s not found.") - - -class AuthorizationFailure(SenlinException): - msg_fmt = _("Authorization failed.") - - class NotAuthenticated(SenlinException): msg_fmt = _("You are not authenticated.") @@ -156,40 +120,10 @@ class Forbidden(SenlinException): msg_fmt = _("You are not authorized to complete this action.") -class NotAuthorized(Forbidden): - msg_fmt = _("You are not authorized to complete this action.") - - -class Invalid(SenlinException): - msg_fmt = _("Data supplied was not valid: %(reason)s") - - -class AuthorizationRedirect(SenlinException): - msg_fmt = _("Redirecting to %(uri)s for authorization.") - - -class RequestUriTooLong(SenlinException): - msg_fmt = _("The URI was too long.") - - class SenlinBadRequest(SenlinException): msg_fmt = _("The request is malformed: %(msg)s") -class MaxRedirectsExceeded(SenlinException): - msg_fmt = _("Maximum redirects (%(redirects)s) was exceeded.") - - -class InvalidRedirect(SenlinException): - msg_fmt = _("Received invalid HTTP redirect.") - - -class RegionAmbiguity(SenlinException): - msg_fmt = _("Multiple 'image' service matches for region %(region)s. This " - "generally means that a region is required and you have not " - "supplied one.") - - class ProjectNotMatch(SenlinException): msg_fmt = _("%(message)s") @@ -199,31 +133,14 @@ class MultipleChoices(SenlinException): "Please be more specific.") -class UserParameterMissing(SenlinException): - msg_fmt = _("The Parameter (%(key)s) was not provided.") - - class InvalidParameter(SenlinException): msg_fmt = _("Invalid value '%(value)s' specified for '%(name)s'") -class InvalidTenant(SenlinException): - msg_fmt = _("Searching Tenant %(target)s " - "from Tenant %(actual)s forbidden.") - - class ClusterNotFound(SenlinException): msg_fmt = _("The cluster (%(cluster)s) could not be found.") -class ClusterExists(SenlinException): - msg_fmt = _("The cluster (%(cluster_name)s) already exists.") - - -class ClusterNotSpecified(SenlinException): - msg_fmt = _("The cluster was not specified.") - - class NodeNotFound(SenlinException): msg_fmt = _("The node (%(node)s) could not be found.") @@ -284,10 +201,6 @@ class NotSupported(SenlinException): msg_fmt = _("%(feature)s is not supported.") -class ClusterActionNotSupported(SenlinException): - msg_fmt = _("%(action)s is not supported for Cluster.") - - class Error(SenlinException): msg_fmt = "%(message)s" @@ -342,24 +255,10 @@ class ActionBeingWorked(SenlinException): msg_fmt = _("Worker %(owner)s is working on this action.") -class StopActionFailed(SenlinException): - msg_fmt = _("Failed to stop cluster (%(cluster_name)s) on other engine " - "(%(engine_id)s)") - - class EventNotFound(SenlinException): msg_fmt = _("The event (%(event)s) could not be found.") -class EventSendFailed(SenlinException): - msg_fmt = _("Failed to send message to cluster (%(cluster_name)s) " - "on other engine (%(engine_id)s)") - - -class DriverFailure(SenlinException): - msg_fmt = _("Driver '%(driver)s' failed creation: %(exc)s") - - class HTTPExceptionDisguise(Exception): """Disguises HTTP exceptions. diff --git a/senlin/db/sqlalchemy/api.py b/senlin/db/sqlalchemy/api.py index 20a43db55..fc4a8c657 100644 --- a/senlin/db/sqlalchemy/api.py +++ b/senlin/db/sqlalchemy/api.py @@ -110,9 +110,8 @@ def _paginate_query(context, query, model, limit=None, marker=None, try: query = utils.paginate_query(query, model, limit, sort_keys, model_marker, sort_dir) - except utils.InvalidSortKey as ex: - # TODO(Qiming): Catch this exception and return to user - raise exception.Invalid(reason=ex.message) + except utils.InvalidSortKey: + raise exception.InvalidParameter(name='sort_keys', value=sort_keys) return query diff --git a/senlin/tests/db/test_cluster_api.py b/senlin/tests/db/test_cluster_api.py index 4164ee9ee..3fa845650 100644 --- a/senlin/tests/db/test_cluster_api.py +++ b/senlin/tests/db/test_cluster_api.py @@ -491,7 +491,7 @@ class DBAPIClusterTest(base.SenlinTestCase): model = mock.Mock() mock_paginate_query.side_effect = db_api.utils.InvalidSortKey() - self.assertRaises(exception.Invalid, db_api._paginate_query, + self.assertRaises(exception.InvalidParameter, db_api._paginate_query, self.ctx, query, model, sort_keys=['foo']) @mock.patch.object(db_api.utils, 'paginate_query') diff --git a/senlin/tests/test_wsgi.py b/senlin/tests/test_wsgi.py index 2bf43c9d8..5331c2e61 100644 --- a/senlin/tests/test_wsgi.py +++ b/senlin/tests/test_wsgi.py @@ -205,8 +205,8 @@ class ResourceTest(base.SenlinTestCase): class ResourceExceptionHandlingTest(base.SenlinTestCase): scenarios = [ ('client_exceptions', dict( - exception=exception.ClusterNotSpecified, - exception_catch=exception.ClusterNotSpecified)), + exception=exception.NotAuthenticated, + exception_catch=exception.NotAuthenticated)), ('webob_bad_request', dict( exception=webob.exc.HTTPBadRequest, exception_catch=exception.HTTPExceptionDisguise)),