]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Do not raise NEW exceptions
authorSergey Vilgelm <svilgelm@mirantis.com>
Mon, 24 Jun 2013 11:26:50 +0000 (15:26 +0400)
committerSergey Vilgelm <svilgelm@mirantis.com>
Tue, 25 Jun 2013 08:27:04 +0000 (12:27 +0400)
Raising NEW exception is bad practice, because we lose TraceBack.
So all places like:

except SomeException as e:
    raise e

should be replaced by

except SomeException:
    raise

If we are doing some other actions before reraising we should
store information about exception then do all actions and then
reraise it. This is caused by eventlet bug. It lost information
about exception if it switch threads.

fixes bug 1191730

Change-Id: Id4aaadde7e69f0bc087cf6d96bb041d53feb131d

quantum/db/db_base_plugin_v2.py
quantum/plugins/cisco/nexus/cisco_nexus_network_driver_v2.py
quantum/plugins/cisco/nexus/cisco_nexus_plugin_v2.py
quantum/plugins/nicira/QuantumPlugin.py
quantum/plugins/nicira/api_client/request.py

index 225ad703a52491106b20e8068732834abf65acc3..3860a5b6cf3a373d943edaf578ab6a7597e27052 100644 (file)
@@ -29,6 +29,7 @@ from quantum.common import exceptions as q_exc
 from quantum.db import api as db
 from quantum.db import models_v2
 from quantum.db import sqlalchemyutils
+from quantum.openstack.common import excutils
 from quantum.openstack.common import log as logging
 from quantum.openstack.common import timeutils
 from quantum.openstack.common import uuidutils
@@ -958,12 +959,12 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2,
                 obj_creator = getattr(self, 'create_%s' % resource)
                 objects.append(obj_creator(context, item))
             context.session.commit()
-        except Exception as e:
-            LOG.exception(_("An exception occured while creating "
+        except Exception:
+            with excutils.save_and_reraise_exception():
+                LOG.error(_("An exception occured while creating "
                             "the %(resource)s:%(item)s"),
                           {'resource': resource, 'item': item})
-            context.session.rollback()
-            raise e
+                context.session.rollback()
         return objects
 
     def _get_marker_obj(self, context, resource, limit, marker):
index b6d9f42d020cb769a0f576b43e3ce038d228a152..a92681a2ebbdd5b37e71270f2549fa86a0bd161e 100644 (file)
@@ -25,6 +25,7 @@ import logging
 
 from ncclient import manager
 
+from quantum.openstack.common import excutils
 from quantum.plugins.cisco.common import cisco_exceptions as cexc
 from quantum.plugins.cisco.db import network_db_v2 as cdb
 from quantum.plugins.cisco.db import nexus_db_v2
@@ -113,13 +114,9 @@ class CiscoNEXUSDriver():
                     config=confstr,
                     allowed_exc_strs=["Can't modify state for extended",
                                       "Command is only allowed on VLAN"])
-            except cexc.NexusConfigFailed as e:
-                # Rollback VLAN creation
-                try:
+            except cexc.NexusConfigFailed:
+                with excutils.save_and_reraise_exception():
                     self.disable_vlan(mgr, vlanid)
-                finally:
-                    # Re-raise original exception
-                    raise e
 
     def disable_vlan(self, mgr, vlanid):
         """Delete a VLAN on Nexus Switch given the VLAN ID."""
index 2ecf381977e853d475877871370de580afd65d2c..f2acd95247155d9ef6d1a5709d621fd86fd5731d 100644 (file)
@@ -27,6 +27,7 @@ PlugIn for Nexus OS driver
 import logging
 
 from quantum.common import exceptions as exc
+from quantum.openstack.common import excutils
 from quantum.openstack.common import importutils
 from quantum.plugins.cisco.common import cisco_constants as const
 from quantum.plugins.cisco.common import cisco_credentials_v2 as cred
@@ -125,8 +126,8 @@ class NexusPlugin(L2DevicePluginBase):
         try:
             nxos_db.add_nexusport_binding(port_id, str(vlan_id),
                                           switch_ip, instance)
-        except Exception as e:
-            try:
+        except Exception:
+            with excutils.save_and_reraise_exception():
                 # Add binding failed, roll back any vlan creation/enabling
                 if vlan_created:
                     self._client.delete_vlan(
@@ -137,9 +138,6 @@ class NexusPlugin(L2DevicePluginBase):
                     self._client.disable_vlan_on_trunk_int(man,
                                                            port_id,
                                                            vlan_id)
-            finally:
-                # Raise the original exception
-                raise e
 
         new_net_dict = {const.NET_ID: net_id,
                         const.NET_NAME: net_name,
@@ -303,19 +301,16 @@ class NexusPlugin(L2DevicePluginBase):
                     str(row['vlan_id']), _nexus_ip,
                     _nexus_username, _nexus_password,
                     _nexus_ports, _nexus_ssh_port)
-            except Exception as e:
+            except Exception:
                 # The delete vlan operation on the Nexus failed,
                 # so this delete_port request has failed. For
                 # consistency, roll back the Nexus database to what
                 # it was before this request.
-                try:
+                with excutils.save_and_reraise_exception():
                     nxos_db.add_nexusport_binding(row['port_id'],
                                                   row['vlan_id'],
                                                   row['switch_ip'],
                                                   row['instance_id'])
-                finally:
-                    # Raise the original exception
-                    raise e
 
         return row['instance_id']
 
index 0c14193479093dd27ff2b145d3df2826903f4e39..5fbea4b4b2e67d7068b7d489ba4d0745ce6081e7 100644 (file)
@@ -51,6 +51,7 @@ from quantum.extensions import l3
 from quantum.extensions import portsecurity as psec
 from quantum.extensions import providernet as pnet
 from quantum.extensions import securitygroup as ext_sg
+from quantum.openstack.common import excutils
 from quantum.openstack.common import importutils
 from quantum.openstack.common import rpc
 from quantum.plugins.nicira.common import config  # noqa
@@ -1215,13 +1216,14 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2,
                 LOG.warning(_("Network %s was not found in NVP."),
                             port_data['network_id'])
                 port_data['status'] = constants.PORT_STATUS_ERROR
-            except Exception as e:
-                # FIXME (arosen) or the plugin_interface call failed in which
-                # case we need to garbage collect the left over port in nvp.
-                err_msg = _("Unable to create port or set port attachment "
-                            "in NVP.")
-                LOG.exception(err_msg)
-                raise e
+            except Exception:
+                with excutils.save_and_reraise_exception():
+                    # FIXME (arosen) or the plugin_interface call failed in
+                    # which case we need to garbage collect the left over
+                    # port in nvp.
+                    err_msg = _("Unable to create port or set port "
+                                "attachment in NVP.")
+                    LOG.error(err_msg)
 
             LOG.debug(_("create_port completed on NVP for tenant "
                         "%(tenant_id)s: (%(id)s)"), port_data)
index 466b7ea01e3ddbeb84824c201f65fdb2d444dc89..d7bd03bfceddaca726449bd4306609979e3eb10c 100644 (file)
@@ -26,9 +26,11 @@ import logging
 import time
 import urlparse
 
+from quantum.openstack.common import excutils
 from quantum.plugins.nicira.api_client.common import (
     _conn_str)
 
+
 logging.basicConfig(level=logging.INFO)
 LOG = logging.getLogger(__name__)
 
@@ -122,9 +124,10 @@ class NvpApiRequest(object):
                 try:
                     conn.request(self._method, url, self._body, headers)
                 except Exception as e:
-                    LOG.warn(_("[%(rid)d] Exception issuing request: %(e)s"),
-                             {'rid': self._rid(), 'e': e})
-                    raise e
+                    with excutils.save_and_reraise_exception():
+                        LOG.warn(_("[%(rid)d] Exception issuing request: "
+                                   "%(e)s"),
+                                 {'rid': self._rid(), 'e': e})
 
                 response = conn.getresponse()
                 response.body = response.read()