From 7219577ba48df211713cab655cdd296a07f35773 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Thu, 9 Aug 2012 22:16:33 -0400 Subject: Make FaultWrapper handle exception code = None. Updates the FaultWrapper middleware so that it properly handles exceptions which contain a code variable set to None. Previously you'd get TypeError exception like this in the api.log file: TypeError: %d format: a number is required, not NoneType This was due to the fact that we tried to format a string with an integer value which was set to None. I hit this today when using Qpid which does in fact throw exceptions which contain 'code' variables set to None. Fixes LP Bug #1035159. Change-Id: I7193031b1f5f9bf84cdb476f8f1268efc50eadf0 --- nova/api/openstack/__init__.py | 2 ++ nova/tests/api/openstack/compute/test_api.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/nova/api/openstack/__init__.py b/nova/api/openstack/__init__.py index 6baa6be91..12dd8ae83 100644 --- a/nova/api/openstack/__init__.py +++ b/nova/api/openstack/__init__.py @@ -52,6 +52,8 @@ class FaultWrapper(base_wsgi.Middleware): safe = getattr(inner, 'safe', False) headers = getattr(inner, 'headers', None) status = getattr(inner, 'code', 500) + if status is None: + status = 500 msg_dict = dict(url=req.url, status=status) LOG.info(_("%(url)s returned with HTTP %(status)d") % msg_dict) diff --git a/nova/tests/api/openstack/compute/test_api.py b/nova/tests/api/openstack/compute/test_api.py index 5bbf1540b..782f1a344 100644 --- a/nova/tests/api/openstack/compute/test_api.py +++ b/nova/tests/api/openstack/compute/test_api.py @@ -176,6 +176,20 @@ class APITest(test.TestCase): self._do_test_exception_mapping(ExceptionWithCode, 'Expectation failed') + def test_exception_with_none_code_throws_500(self): + class ExceptionWithNoneCode(Exception): + code = None + + msg = 'Internal Server Error' + + @webob.dec.wsgify + def fail(req): + raise ExceptionWithNoneCode() + + api = self._wsgi_app(fail) + resp = webob.Request.blank('/').get_response(api) + self.assertEqual(500, resp.status_int) + def test_request_id_in_response(self): req = webob.Request.blank('/') req.method = 'GET' -- cgit