summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Elliott <brian.elliott@rackspace.com>2012-08-09 22:51:11 +0000
committerBrian Elliott <brian.elliott@rackspace.com>2012-08-12 00:40:18 +0000
commit5646b96119947ba1b3086ac598d9c97bee3fca58 (patch)
tree2dd0cdc1e433e4f2ec107b5d24298d2315cf2adc
parent7c9bb06622c72e5ec823f15a98cd9ea93d094f2a (diff)
Fix TypeError conversion in API layer
Fix conversion of TypeError to Fault in ResourceExceptionHandler. TypeError can result when the list of extensions is inovked if the parameters don't match the extension method's signature. Specifically, if an empty body was sent with an HTTP POST to create a server, a 500 error was returned. This change is a fix to properly return a 400 instead. (In Python2.6, the ex_value argument of __exit__ is type string when ex_type is a TypeError, which caused the conversion logic to get skipped.) bug 1035120 Change-Id: I96ad335a6338523345d28b7e744dbc7449b4753d
-rw-r--r--nova/api/openstack/wsgi.py6
-rw-r--r--nova/tests/api/openstack/test_wsgi.py12
2 files changed, 17 insertions, 1 deletions
diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py
index 3ea7c1c7c..9f8bf6367 100644
--- a/nova/api/openstack/wsgi.py
+++ b/nova/api/openstack/wsgi.py
@@ -628,7 +628,11 @@ class ResourceExceptionHandler(object):
elif isinstance(ex_value, exception.Invalid):
raise Fault(exception.ConvertedException(
code=ex_value.code, explanation=unicode(ex_value)))
- elif isinstance(ex_value, TypeError):
+
+ # Under python 2.6, TypeError's exception value is actually a string,
+ # so test # here via ex_type instead:
+ # http://bugs.python.org/issue7853
+ elif issubclass(ex_type, TypeError):
exc_info = (ex_type, ex_value, ex_traceback)
LOG.error(_('Exception handling resource: %s') % ex_value,
exc_info=exc_info)
diff --git a/nova/tests/api/openstack/test_wsgi.py b/nova/tests/api/openstack/test_wsgi.py
index f45450495..7427eb2af 100644
--- a/nova/tests/api/openstack/test_wsgi.py
+++ b/nova/tests/api/openstack/test_wsgi.py
@@ -752,6 +752,18 @@ class ResourceTest(test.TestCase):
self.assertEqual(called, [2])
self.assertEqual(response, 'foo')
+ def test_resource_exception_handler_type_error(self):
+ """A TypeError should be translated to a Fault/HTTP 400"""
+ def foo(a,):
+ return a
+
+ try:
+ with wsgi.ResourceExceptionHandler():
+ foo() # generate a TypeError
+ self.fail("Should have raised a Fault (HTTP 400)")
+ except wsgi.Fault as fault:
+ self.assertEqual(400, fault.status_int)
+
class ResponseObjectTest(test.TestCase):
def test_default_code(self):