[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
This commit is contained in:
Chris Dent 2016-09-22 16:03:21 +00:00
parent 8dc6b097bb
commit bc4370c7db
2 changed files with 23 additions and 4 deletions

View File

@ -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

View File

@ -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()