Merge "HTTP response json body fix"
This commit is contained in:
commit
8c252c0df4
@ -63,7 +63,7 @@ class PecanTransportDriver(transport.Driver):
|
||||
home_controller.add_controller('services', v1.Services(self))
|
||||
home_controller.add_controller('flavors', v1.Flavors(self))
|
||||
|
||||
pecan_hooks = [hooks.Context()]
|
||||
pecan_hooks = [hooks.Context(), hooks.Error()]
|
||||
self._app = pecan.make_app(root_controller, hooks=pecan_hooks)
|
||||
|
||||
def listen(self):
|
||||
|
@ -16,7 +16,9 @@
|
||||
"""Pecan Hooks"""
|
||||
|
||||
from poppy.transport.pecan.hooks import context
|
||||
from poppy.transport.pecan.hooks import error
|
||||
|
||||
|
||||
# Hoist into package namespace
|
||||
Context = context.ContextHook
|
||||
Error = error.ErrorHook
|
||||
|
69
poppy/transport/pecan/hooks/error.py
Normal file
69
poppy/transport/pecan/hooks/error.py
Normal file
@ -0,0 +1,69 @@
|
||||
# Copyright (c) 2014 Rackspace, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
from pecan import hooks
|
||||
import webob
|
||||
|
||||
from poppy.openstack.common import log as oslo_log
|
||||
|
||||
LOG = oslo_log.getLogger(__name__)
|
||||
|
||||
|
||||
class ErrorHook(hooks.PecanHook):
|
||||
|
||||
'''Intercepts all errors during request fulfillment and logs them.'''
|
||||
|
||||
def on_error(self, state, exception):
|
||||
'''Fires off when an error happens during a request.
|
||||
|
||||
:param state: The Pecan state for the current request.
|
||||
:type state: pecan.core.state
|
||||
:param exception: The exception that was raised.
|
||||
:type exception: Exception
|
||||
:returns: webob.Response -- JSON response with the error
|
||||
message.
|
||||
'''
|
||||
exception_payload = {
|
||||
'status': 500,
|
||||
}
|
||||
message = {
|
||||
'message': (
|
||||
'The server encountered an unexpected condition'
|
||||
' which prevented it from fulfilling the request.'
|
||||
)
|
||||
}
|
||||
log_level = logging.ERROR
|
||||
|
||||
# poppy does not have internal exception/eternal exceptions
|
||||
# yet, so just catch the HTTPException from pecan.abort
|
||||
# and send out a a json body
|
||||
message['message'] = str(exception)
|
||||
exception_payload['status'] = exception.status
|
||||
|
||||
LOG.log(
|
||||
log_level,
|
||||
'Exception message: %s' % str(exception),
|
||||
exc_info=True,
|
||||
extra=exception_payload
|
||||
)
|
||||
|
||||
return webob.Response(
|
||||
json.dumps(message),
|
||||
status=exception_payload['status'],
|
||||
content_type='application/json'
|
||||
)
|
43
tests/functional/transport/pecan/hooks/test_error.py
Normal file
43
tests/functional/transport/pecan/hooks/test_error.py
Normal file
@ -0,0 +1,43 @@
|
||||
# Copyright (c) 2014 Rackspace, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from tests.functional.transport.pecan import base
|
||||
|
||||
|
||||
class ErrorHookTest(base.FunctionalTest):
|
||||
|
||||
def setUp(self):
|
||||
super(ErrorHookTest, self).setUp()
|
||||
|
||||
self.headers = {'X-Auth-Token': str(uuid.uuid4())}
|
||||
|
||||
def test_404_error(self):
|
||||
self.headers['X-Project-Id'] = '000001'
|
||||
# use try except to actually catch the response body in
|
||||
# exception part
|
||||
response = self.app.get(
|
||||
'/v1.0/services/non_exist_service_name',
|
||||
headers=self.headers,
|
||||
status=404)
|
||||
|
||||
self.assertEqual(404, response.status_code)
|
||||
self.assertEqual('application/json', response.content_type)
|
||||
response_json = json.loads(response.body.decode('utf-8'))
|
||||
self.assertTrue('message' in response_json)
|
||||
self.assertEqual('service non_exist_service_name is not found',
|
||||
response_json['message'])
|
Loading…
Reference in New Issue
Block a user