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:
Sergey Kraynev
2025-11-08 21:19:17 +04:00
parent 036f092820
commit e3894e4efb
2 changed files with 15 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ import copy
from neutron_lib import constants as const
from oslo_log import log as logging
from oslo_policy import policy as oslo_policy
from oslo_serialization import jsonutils
from oslo_utils import excutils
from pecan import hooks
import webob
@@ -195,6 +196,14 @@ class PolicyHook(hooks.PecanHook):
# we have to set the status_code here to prevent the catch_errors
# middleware from turning this into a 500.
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
if is_single:

View File

@@ -288,6 +288,12 @@ class TestPolicyEnforcementHook(test_functional.PecanFunctionalTest):
headers={'X-Project-Id': 'tenid'},
expect_errors=True)
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)
def test_after_on_get_excludes_admin_attribute(self):