Replace response body in after hook for 404 error
Closes-Bug: 2130972
Change-Id: I662b3eb7fb8c17d0ad30b10e8537d4a142003256
Signed-off-by: Sergey Kraynev <sergejyit@gmail.com>
(cherry picked from commit d95e3e5eaf)
This commit is contained in:
@@ -18,6 +18,7 @@ import copy
|
|||||||
from neutron_lib import constants as const
|
from neutron_lib import constants as const
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_policy import policy as oslo_policy
|
from oslo_policy import policy as oslo_policy
|
||||||
|
from oslo_serialization import jsonutils
|
||||||
from oslo_utils import excutils
|
from oslo_utils import excutils
|
||||||
from pecan import hooks
|
from pecan import hooks
|
||||||
import webob
|
import webob
|
||||||
@@ -195,6 +196,14 @@ class PolicyHook(hooks.PecanHook):
|
|||||||
# we have to set the status_code here to prevent the catch_errors
|
# we have to set the status_code here to prevent the catch_errors
|
||||||
# middleware from turning this into a 500.
|
# middleware from turning this into a 500.
|
||||||
state.response.status_code = 404
|
state.response.status_code = 404
|
||||||
|
# replace the original body on NotFound body
|
||||||
|
error_message = {
|
||||||
|
'type': 'HTTPNotFound',
|
||||||
|
'message': 'The resource could not be found.',
|
||||||
|
'detail': ''
|
||||||
|
}
|
||||||
|
state.response.text = jsonutils.dumps(error_message)
|
||||||
|
state.response.content_type = 'application/json'
|
||||||
return
|
return
|
||||||
|
|
||||||
if is_single:
|
if is_single:
|
||||||
|
|||||||
@@ -288,6 +288,12 @@ class TestPolicyEnforcementHook(test_functional.PecanFunctionalTest):
|
|||||||
headers={'X-Project-Id': 'tenid'},
|
headers={'X-Project-Id': 'tenid'},
|
||||||
expect_errors=True)
|
expect_errors=True)
|
||||||
self.assertEqual(404, response.status_int)
|
self.assertEqual(404, response.status_int)
|
||||||
|
self.assertEqual(
|
||||||
|
{
|
||||||
|
'type': 'HTTPNotFound',
|
||||||
|
'message': 'The resource could not be found.',
|
||||||
|
'detail': ''
|
||||||
|
}, jsonutils.loads(response.body))
|
||||||
self.assertEqual(1, self.mock_plugin.get_meh.call_count)
|
self.assertEqual(1, self.mock_plugin.get_meh.call_count)
|
||||||
|
|
||||||
def test_after_on_get_excludes_admin_attribute(self):
|
def test_after_on_get_excludes_admin_attribute(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user