Fix exception raised in exception wrapper

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
This commit is contained in:
Diana Clarke 2016-10-24 13:23:14 -04:00
parent 7e36553fa1
commit f98e4aa5b7
2 changed files with 33 additions and 1 deletions

View 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))

View 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')