From 9b50a25c2817f804b6898f04eccfcebf895e82da Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 30 Jul 2013 13:16:51 +0200 Subject: [PATCH] Remove openstack.common.exception usage This file is deprecated, stop using it. Change-Id: I5d79b8c0134bb5f4021487542d4d0a964281b8bf Fixes-Bug: #1208734 --- neutron/common/exceptions.py | 30 ++++-- neutron/neutron_plugin_base_v2.py | 8 +- neutron/openstack/common/exception.py | 141 -------------------------- neutron/quota.py | 2 +- neutron/tests/base.py | 5 +- neutron/tests/unit/test_api_v2.py | 2 +- openstack-common.conf | 1 - 7 files changed, 28 insertions(+), 161 deletions(-) delete mode 100644 neutron/openstack/common/exception.py diff --git a/neutron/common/exceptions.py b/neutron/common/exceptions.py index 408d609c58..68c73f50bb 100644 --- a/neutron/common/exceptions.py +++ b/neutron/common/exceptions.py @@ -19,12 +19,10 @@ Neutron base exception handling. """ -from neutron.openstack.common.exception import Error -from neutron.openstack.common.exception import InvalidContentType # noqa -from neutron.openstack.common.exception import OpenstackException +_FATAL_EXCEPTION_FORMAT_ERRORS = False -class NeutronException(OpenstackException): +class NeutronException(Exception): """Base Neutron Exception. To correctly use this class, inherit from it and define @@ -33,6 +31,16 @@ class NeutronException(OpenstackException): """ message = _("An unknown exception occurred.") + def __init__(self, **kwargs): + try: + super(NeutronException, self).__init__(self.message % kwargs) + except Exception: + if _FATAL_EXCEPTION_FORMAT_ERRORS: + raise + else: + # at least get the core message out if something happened + super(NeutronException, self).__init__(self.message) + class BadRequest(NeutronException): message = _('Bad %(resource)s request: %(msg)s') @@ -185,8 +193,10 @@ class MalformedRequestBody(BadRequest): message = _("Malformed request body: %(reason)s") -class Invalid(Error): - pass +class Invalid(NeutronException): + def __init__(self, message=None): + self.message = message + super(Invalid, self).__init__() class InvalidInput(BadRequest): @@ -207,10 +217,6 @@ class OutOfBoundsAllocationPool(BadRequest): "beyond the subnet cidr %(subnet_cidr)s.") -class NotImplementedError(Error): - pass - - class MacAddressGenerationFailure(ServiceUnavailable): message = _("Unable to generate unique mac on network %(net_id)s.") @@ -257,6 +263,10 @@ class InvalidExtensionEnv(BadRequest): message = _("Invalid extension environment: %(reason)s") +class InvalidContentType(NeutronException): + message = "Invalid content type %(content_type)s" + + class ExternalIpAddressExhausted(BadRequest): message = _("Unable to find any IP address on external " "network %(net_id)s.") diff --git a/neutron/neutron_plugin_base_v2.py b/neutron/neutron_plugin_base_v2.py index 9367c29f50..fb60ffe599 100644 --- a/neutron/neutron_plugin_base_v2.py +++ b/neutron/neutron_plugin_base_v2.py @@ -23,8 +23,6 @@ methods that needs to be implemented by a v2 Neutron Plug-in. from abc import ABCMeta, abstractmethod -from neutron.common import exceptions - class NeutronPluginBaseV2(object): @@ -119,7 +117,7 @@ class NeutronPluginBaseV2(object): .. note:: this method is optional, as it was not part of the originally defined plugin API. """ - raise exceptions.NotImplementedError() + raise NotImplementedError @abstractmethod def delete_subnet(self, context, id): @@ -220,7 +218,7 @@ class NeutronPluginBaseV2(object): NOTE: this method is optional, as it was not part of the originally defined plugin API. """ - raise exceptions.NotImplementedError() + raise NotImplementedError @abstractmethod def delete_network(self, context, id): @@ -316,7 +314,7 @@ class NeutronPluginBaseV2(object): .. note:: this method is optional, as it was not part of the originally defined plugin API. """ - raise exceptions.NotImplementedError() + raise NotImplementedError @abstractmethod def delete_port(self, context, id): diff --git a/neutron/openstack/common/exception.py b/neutron/openstack/common/exception.py deleted file mode 100644 index a1cf6bb900..0000000000 --- a/neutron/openstack/common/exception.py +++ /dev/null @@ -1,141 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2011 OpenStack Foundation. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -""" -Exceptions common to OpenStack projects -""" - -import logging - -from neutron.openstack.common.gettextutils import _ - -_FATAL_EXCEPTION_FORMAT_ERRORS = False - - -class Error(Exception): - def __init__(self, message=None): - super(Error, self).__init__(message) - - -class ApiError(Error): - def __init__(self, message='Unknown', code='Unknown'): - self.message = message - self.code = code - super(ApiError, self).__init__('%s: %s' % (code, message)) - - -class NotFound(Error): - pass - - -class UnknownScheme(Error): - - msg = "Unknown scheme '%s' found in URI" - - def __init__(self, scheme): - msg = self.__class__.msg % scheme - super(UnknownScheme, self).__init__(msg) - - -class BadStoreUri(Error): - - msg = "The Store URI %s was malformed. Reason: %s" - - def __init__(self, uri, reason): - msg = self.__class__.msg % (uri, reason) - super(BadStoreUri, self).__init__(msg) - - -class Duplicate(Error): - pass - - -class NotAuthorized(Error): - pass - - -class NotEmpty(Error): - pass - - -class Invalid(Error): - pass - - -class BadInputError(Exception): - """Error resulting from a client sending bad input to a server""" - pass - - -class MissingArgumentError(Error): - pass - - -class DatabaseMigrationError(Error): - pass - - -class ClientConnectionError(Exception): - """Error resulting from a client connecting to a server""" - pass - - -def wrap_exception(f): - def _wrap(*args, **kw): - try: - return f(*args, **kw) - except Exception as e: - if not isinstance(e, Error): - #exc_type, exc_value, exc_traceback = sys.exc_info() - logging.exception(_('Uncaught exception')) - #logging.error(traceback.extract_stack(exc_traceback)) - raise Error(str(e)) - raise - _wrap.func_name = f.func_name - return _wrap - - -class OpenstackException(Exception): - """Base Exception class. - - To correctly use this class, inherit from it and define - a 'message' property. That message will get printf'd - with the keyword arguments provided to the constructor. - """ - message = "An unknown exception occurred" - - def __init__(self, **kwargs): - try: - self._error_string = self.message % kwargs - - except Exception as e: - if _FATAL_EXCEPTION_FORMAT_ERRORS: - raise e - else: - # at least get the core message out if something happened - self._error_string = self.message - - def __str__(self): - return self._error_string - - -class MalformedRequestBody(OpenstackException): - message = "Malformed message body: %(reason)s" - - -class InvalidContentType(OpenstackException): - message = "Invalid content type %(content_type)s" diff --git a/neutron/quota.py b/neutron/quota.py index fd8c3b3426..098a1599de 100644 --- a/neutron/quota.py +++ b/neutron/quota.py @@ -300,7 +300,7 @@ def _count_resource(context, plugin, resources, tenant_id): try: obj_count_getter = getattr(plugin, count_getter_name) return obj_count_getter(context, filters={'tenant_id': [tenant_id]}) - except (exceptions.NotImplementedError, AttributeError): + except (NotImplementedError, AttributeError): obj_getter = getattr(plugin, "get_%s" % resources) obj_list = obj_getter(context, filters={'tenant_id': [tenant_id]}) return len(obj_list) if obj_list else 0 diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 3829b55121..3bb5a2e093 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -25,7 +25,8 @@ from oslo.config import cfg import stubout import testtools -from neutron.openstack.common import exception +from neutron.common import exceptions + CONF = cfg.CONF TRUE_STRING = ['True', '1'] @@ -62,7 +63,7 @@ class BaseTestCase(testtools.TestCase): stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) self.stubs = stubout.StubOutForTesting() - self.stubs.Set(exception, '_FATAL_EXCEPTION_FORMAT_ERRORS', True) + self.stubs.Set(exceptions, '_FATAL_EXCEPTION_FORMAT_ERRORS', True) def config(self, **kw): """Override some configuration values. diff --git a/neutron/tests/unit/test_api_v2.py b/neutron/tests/unit/test_api_v2.py index 200aa2966b..7d4d547306 100644 --- a/neutron/tests/unit/test_api_v2.py +++ b/neutron/tests/unit/test_api_v2.py @@ -1325,7 +1325,7 @@ class QuotaTest(APIv2TestBase): instance = self.plugin.return_value instance.get_networks_count.side_effect = ( - q_exc.NotImplementedError()) + NotImplementedError()) instance.get_networks.return_value = ["foo"] res = self.api.post_json( _get_path('networks'), initial_input, expect_errors=True) diff --git a/openstack-common.conf b/openstack-common.conf index ab9ae0d090..7e1c867856 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -4,7 +4,6 @@ module=context module=db module=db.sqlalchemy module=eventlet_backdoor -module=exception module=excutils module=fileutils module=gettextutils