]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
Fix exception raised in exception wrapper
authorDiana Clarke <diana.joan.clarke@gmail.com>
Mon, 24 Oct 2016 17:23:14 +0000 (13:23 -0400)
committerDiana Clarke <diana.joan.clarke@gmail.com>
Mon, 24 Oct 2016 17:24:14 +0000 (13:24 -0400)
In some cases the exception wrapper can't find the module name for a
traceback (like lxml c extensions), resulting in an exception from
inspect.getmodule(). Just set the module name to 'unknown' in these
cases.

Change-Id: Id5e181d682598eab6987ad6f0f194c77e061f69c
Closes-Bug: #1635467

nova/notifications/objects/exception.py
nova/tests/unit/test_exception.py

index b0ee28f7ceb6b4b9ea339c5510029cee3f6e7081..b769c8b929f2440e760279c63ae1175cd745a369 100644 (file)
@@ -35,9 +35,11 @@ class ExceptionPayload(base.NotificationPayloadBase):
         # TODO(gibi): apply strutils.mask_password on exception_message and
         # consider emitting the exception_message only if the safe flag is
         # true in the exception like in the REST API
+        module = inspect.getmodule(trace[0])
+        module_name = module.__name__ if module else 'unknown'
         return cls(
                 function_name=trace[3],
-                module_name=inspect.getmodule(trace[0]).__name__,
+                module_name=module_name,
                 exception=fault.__class__.__name__,
                 exception_message=six.text_type(fault))
 
index a9bada1af6fd83567fc7901e7efed715eac5053c..b4ff568e15535b4180b55b2123040a5080855425 100644 (file)
@@ -35,6 +35,25 @@ def bad_function_exception(self, context, extra, blah="a", boo="b", zoo=None):
     raise test.TestingException('bad things happened')
 
 
+def bad_function_unknown_module(self, context):
+    """Example traceback that points to a module that getmodule() can't find.
+
+    Traceback (most recent call last):
+      File "<stdin>", line 1, in <module>
+      File "src/lxml/lxml.etree.pyx", line 2402, in
+          lxml.etree._Attrib.__setitem__ (src/lxml/lxml.etree.c:67548)
+      File "src/lxml/apihelpers.pxi", line 570, in
+          lxml.etree._setAttributeValue (src/lxml/lxml.etree.c:21551)
+      File "src/lxml/apihelpers.pxi", line 1437, in
+          lxml.etree._utf8 (src/lxml/lxml.etree.c:30194)
+    TypeError: Argument must be bytes or unicode, got 'NoneType'
+
+    """
+    from lxml import etree
+    x = etree.fromstring('<hello/>')
+    x.attrib['foo'] = None
+
+
 class WrapExceptionTestCase(test.NoDBTestCase):
     def setUp(self):
         super(WrapExceptionTestCase, self).setUp()
@@ -47,6 +66,17 @@ class WrapExceptionTestCase(test.NoDBTestCase):
         self.assertEqual(0, len(fake_notifier.NOTIFICATIONS))
         self.assertEqual(0, len(fake_notifier.VERSIONED_NOTIFICATIONS))
 
+    def test_wrap_exception_unknown_module(self):
+        ctxt = context.get_admin_context()
+        wrapped = exception_wrapper.wrap_exception(
+            rpc.get_notifier('fake'), binary='fake-binary')
+        self.assertRaises(
+            TypeError, wrapped(bad_function_unknown_module), None, ctxt)
+        self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS))
+        notification = fake_notifier.VERSIONED_NOTIFICATIONS[0]
+        payload = notification['payload']['nova_object.data']
+        self.assertEqual('unknown', payload['module_name'])
+
     def test_wrap_exception_with_notifier(self):
         wrapped = exception_wrapper.wrap_exception(rpc.get_notifier('fake'),
                                                    binary='fake-binary')