diff --git a/openstack/cloud/_network.py b/openstack/cloud/_network.py index 7779e1062..42ca1ffd9 100644 --- a/openstack/cloud/_network.py +++ b/openstack/cloud/_network.py @@ -1863,12 +1863,8 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): ) policy = self.network.find_qos_policy( - policy_name_or_id, ignore_missing=True + policy_name_or_id, ignore_missing=False ) - if not policy: - raise exceptions.NotFoundException( - f"QoS policy {policy_name_or_id} not Found." - ) return self.network.get_qos_minimum_bandwidth_rule(rule_id, policy) @@ -1897,12 +1893,8 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): ) policy = self.network.find_qos_policy( - policy_name_or_id, ignore_missing=True + policy_name_or_id, ignore_missing=False ) - if not policy: - raise exceptions.NotFoundException( - f"QoS policy {policy_name_or_id} not Found." - ) kwargs['min_kbps'] = min_kbps @@ -1931,12 +1923,8 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): ) policy = self.network.find_qos_policy( - policy_name_or_id, ignore_missing=True + policy_name_or_id, ignore_missing=False ) - if not policy: - raise exceptions.NotFoundException( - f"QoS policy {policy_name_or_id} not Found." - ) if not kwargs: self.log.debug("No QoS minimum bandwidth rule data to update") @@ -1971,12 +1959,8 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): ) policy = self.network.find_qos_policy( - policy_name_or_id, ignore_missing=True + policy_name_or_id, ignore_missing=False ) - if not policy: - raise exceptions.NotFoundException( - f"QoS policy {policy_name_or_id} not Found." - ) try: self.network.delete_qos_minimum_bandwidth_rule( diff --git a/openstack/exceptions.py b/openstack/exceptions.py index a1419052e..4b068399d 100644 --- a/openstack/exceptions.py +++ b/openstack/exceptions.py @@ -19,10 +19,13 @@ Exception definitions. import json import re import typing as ty +import warnings import requests from requests import exceptions as _rex +from openstack import warnings as os_warnings + if ty.TYPE_CHECKING: from openstack import resource @@ -73,13 +76,31 @@ class HttpException(SDKException, _rex.HTTPError): details: ty.Optional[str] = None, request_id: ty.Optional[str] = None, ): - # TODO(shade) Remove http_status parameter and the ability for response - # to be None once we're not mocking Session everywhere. + if http_status is not None: + warnings.warn( + "The 'http_status' parameter is unnecessary and will be " + "removed in a future release", + os_warnings.RemovedInSDK50Warning, + ) + + if request_id is not None: + warnings.warn( + "The 'request_id' parameter is unnecessary and will be " + "removed in a future release", + os_warnings.RemovedInSDK50Warning, + ) + if not message: if response is not None: message = f"{self.__class__.__name__}: {response.status_code}" else: message = f"{self.__class__.__name__}: Unknown error" + status = ( + response.status_code + if response is not None + else 'Unknown error' + ) + message = f'{self.__class__.__name__}: {status}' # Call directly rather than via super to control parameters SDKException.__init__(self, message=message) @@ -241,15 +262,10 @@ def raise_from_response( if not details: details = response.reason if response.reason else response.text - http_status = response.status_code - request_id = response.headers.get('x-openstack-request-id') - raise cls( message=error_message, response=response, details=details, - http_status=http_status, - request_id=request_id, ) diff --git a/openstack/tests/unit/test_exceptions.py b/openstack/tests/unit/test_exceptions.py index 86a2b62f6..1f11f218a 100644 --- a/openstack/tests/unit/test_exceptions.py +++ b/openstack/tests/unit/test_exceptions.py @@ -13,9 +13,11 @@ import json from unittest import mock import uuid +import warnings from openstack import exceptions from openstack.tests.unit import base +from openstack.tests.unit import fakes class Test_Exception(base.TestCase): @@ -23,7 +25,7 @@ class Test_Exception(base.TestCase): exc = exceptions.MethodNotSupported(self.__class__, 'list') expected = ( 'The list method is not supported for ' - + 'openstack.tests.unit.test_exceptions.Test_Exception' + 'openstack.tests.unit.test_exceptions.Test_Exception' ) self.assertEqual(expected, str(exc)) @@ -31,14 +33,29 @@ class Test_Exception(base.TestCase): class Test_HttpException(base.TestCase): def setUp(self): super().setUp() - self.message = "mayday" + self.message = 'mayday' + self.response = fakes.FakeResponse( + status_code=401, + data={ + 'error': { + 'code': 401, + 'message': ( + 'The request you have made requires authentication.' + ), + 'title': 'Unauthorized', + }, + }, + ) def _do_raise(self, *args, **kwargs): raise exceptions.HttpException(*args, **kwargs) def test_message(self): exc = self.assertRaises( - exceptions.HttpException, self._do_raise, self.message + exceptions.HttpException, + self._do_raise, + self.message, + response=self.response, ) self.assertEqual(self.message, exc.message) @@ -49,6 +66,7 @@ class Test_HttpException(base.TestCase): exceptions.HttpException, self._do_raise, self.message, + response=self.response, details=details, ) @@ -57,16 +75,24 @@ class Test_HttpException(base.TestCase): def test_http_status(self): http_status = 123 - exc = self.assertRaises( - exceptions.HttpException, - self._do_raise, - self.message, - http_status=http_status, - ) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + + exc = self.assertRaises( + exceptions.HttpException, + self._do_raise, + self.message, + http_status=http_status, + ) self.assertEqual(self.message, exc.message) self.assertEqual(http_status, exc.status_code) + self.assertIn( + "The 'http_status' parameter is unnecessary", + str(w[-1]), + ) + class TestRaiseFromResponse(base.TestCase): def setUp(self): diff --git a/openstack/tests/unit/test_proxy.py b/openstack/tests/unit/test_proxy.py index 431d70d77..cfb09036f 100644 --- a/openstack/tests/unit/test_proxy.py +++ b/openstack/tests/unit/test_proxy.py @@ -21,6 +21,7 @@ from openstack import exceptions from openstack import proxy from openstack import resource from openstack.tests.unit import base +from openstack.tests.unit import fakes from openstack import utils @@ -195,7 +196,8 @@ class TestProxyDelete(base.TestCase): def test_delete_ignore_missing(self): self.res.delete.side_effect = exceptions.NotFoundException( - message="test", http_status=404 + message="test", + response=fakes.FakeResponse(status_code=404, data={'error': None}), ) rv = self.sot._delete(DeleteableResource, self.fake_id) @@ -203,7 +205,8 @@ class TestProxyDelete(base.TestCase): def test_delete_NotFound(self): self.res.delete.side_effect = exceptions.NotFoundException( - message="test", http_status=404 + message="test", + response=fakes.FakeResponse(status_code=404, data={'error': None}), ) self.assertRaisesRegex( @@ -217,8 +220,9 @@ class TestProxyDelete(base.TestCase): ) def test_delete_HttpException(self): - self.res.delete.side_effect = exceptions.HttpException( - message="test", http_status=500 + self.res.delete.side_effect = exceptions.ResourceNotFound( + message="test", + response=fakes.FakeResponse(status_code=500, data={'error': None}), ) self.assertRaises( @@ -426,7 +430,8 @@ class TestProxyGet(base.TestCase): def test_get_not_found(self): self.res.fetch.side_effect = exceptions.NotFoundException( - message="test", http_status=404 + message="test", + response=fakes.FakeResponse(status_code=404, data={'error': None}), ) self.assertRaisesRegex(