From bc4370c7db470a111727742a0b0f29b47bdfd8aa Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Thu, 22 Sep 2016 16:03:21 +0000 Subject: [PATCH] [placement] 404 responses do not cause exception logs Prior to this change, a 404 caused by a not possible route being requested (such as /placement/hello) would result in a proper 404 response but also a log message about an uncaught exception. This made it appear that a 404 response is unexpected and something that needs exceptional attention. It doesn't. This change makes sure that 404s propagate up the stack in a clean way and are logged only by the existing INFO log used for all requests. Change-Id: I21f9411bd5154eddc582d5f38891be747de874b3 Closes-Bug: #1626489 --- nova/api/openstack/placement/handler.py | 12 ++++++++---- .../unit/api/openstack/placement/test_handler.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/nova/api/openstack/placement/handler.py b/nova/api/openstack/placement/handler.py index 393b23412..5d5377f4c 100644 --- a/nova/api/openstack/placement/handler.py +++ b/nova/api/openstack/placement/handler.py @@ -169,13 +169,17 @@ class PlacementHandler(object): json_formatter=util.json_error_formatter) try: return dispatch(environ, start_response, self._map) - # Trap the small number of nova exceptions that aren't - # caught elsewhere and transform them into webob.exc. - # These are common exceptions raised when making calls against - # nova.objects in the handlers. + # Trap the NotFound exceptions raised by the objects used + # with the API and transform them into webob.exc.HTTPNotFound. except exception.NotFound as exc: raise webob.exc.HTTPNotFound( exc, json_formatter=util.json_error_formatter) + # Trap the HTTPNotFound that can be raised by dispatch() + # when no route is found. The exception is passed through to + # the FaultWrap middleware without causing an alarming log + # message. + except webob.exc.HTTPNotFound: + raise except Exception as exc: LOG.exception(_LE("Uncaught exception")) raise diff --git a/nova/tests/unit/api/openstack/placement/test_handler.py b/nova/tests/unit/api/openstack/placement/test_handler.py index 38da37b65..b0a81e15e 100644 --- a/nova/tests/unit/api/openstack/placement/test_handler.py +++ b/nova/tests/unit/api/openstack/placement/test_handler.py @@ -97,3 +97,18 @@ class MapperTest(test.NoDBTestCase): result = self.mapper.match(environ=environ) self.assertEqual(handler.handle_405, result['action']) self.assertEqual('GET', result['_methods']) + + +class PlacementLoggingTest(test.NoDBTestCase): + + @mock.patch("nova.api.openstack.placement.handler.LOG") + def test_404_no_error_log(self, mocked_log): + environ = _environ(path='/hello', method='GET') + context_mock = mock.Mock() + context_mock.to_policy_values.return_value = {'roles': ['admin']} + environ['placement.context'] = context_mock + app = handler.PlacementHandler() + self.assertRaises(webob.exc.HTTPNotFound, + app, environ, start_response) + mocked_log.error.assert_not_called() + mocked_log.exception.assert_not_called()