From 0c154ca94438e26884770742822728ecde3810e0 Mon Sep 17 00:00:00 2001 From: John Schwarz Date: Mon, 3 Aug 2015 16:56:27 +0300 Subject: [PATCH] Gracefully handle fetching nonexistent rule Currently, if we invoke the API for 'show rule' but the rule does not exist, an exception is raised from deep within Neutron. This in turns causes an uncaught exception and the user will see 'ServerFault'. This patch proposes a fix for this scenario - the case where the rule does not exist is handled and a NeutronException is caused, causing a proper 'NotFound' error on the client side instead. Partially-Implements: blueprint quantum-qos-api Change-Id: Ic703a0865d1cfa057ab1ad5290b793b22df06af6 --- neutron/common/exceptions.py | 5 +++++ neutron/services/qos/qos_plugin.py | 6 ++++-- neutron/tests/api/test_qos.py | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/neutron/common/exceptions.py b/neutron/common/exceptions.py index 7dc39bf48..40bb50e7c 100644 --- a/neutron/common/exceptions.py +++ b/neutron/common/exceptions.py @@ -97,6 +97,11 @@ class QosPolicyNotFound(NotFound): message = _("QoS policy %(policy_id)s could not be found") +class QosRuleNotFound(NotFound): + message = _("QoS rule %(rule_id)s for policy %(policy_id)s " + "could not be found") + + class PortNotFoundOnNetwork(NotFound): message = _("Port %(port_id)s could not be found " "on network %(net_id)s") diff --git a/neutron/services/qos/qos_plugin.py b/neutron/services/qos/qos_plugin.py index d66acc268..8a11499e5 100644 --- a/neutron/services/qos/qos_plugin.py +++ b/neutron/services/qos/qos_plugin.py @@ -128,8 +128,10 @@ class QoSPlugin(qos.QoSPluginBase): policy_id, fields=None): # validate that we have access to the policy self._get_policy_obj(context, policy_id) - return rule_object.QosBandwidthLimitRule.get_by_id(context, - rule_id) + rule = rule_object.QosBandwidthLimitRule.get_by_id(context, rule_id) + if not rule: + raise n_exc.QosRuleNotFound(policy_id=policy_id, rule_id=rule_id) + return rule @db_base_plugin_common.filter_fields @db_base_plugin_common.convert_result_to_dict diff --git a/neutron/tests/api/test_qos.py b/neutron/tests/api/test_qos.py index f476ecf1d..9a043148f 100644 --- a/neutron/tests/api/test_qos.py +++ b/neutron/tests/api/test_qos.py @@ -294,7 +294,7 @@ class QosBandwidthLimitRuleTestJSON(base.BaseAdminNetworkTest): self.assertEqual(rule['id'], retrieved_policy['id']) self.admin_client.delete_bandwidth_limit_rule(policy['id'], rule['id']) - self.assertRaises(exceptions.ServerFault, + self.assertRaises(exceptions.NotFound, self.admin_client.show_bandwidth_limit_rule, policy['id'], rule['id']) -- 2.45.2