From db616b4cf755b73928cc9ba71bb2c1bdd43161e2 Mon Sep 17 00:00:00 2001 From: Devananda van der Veen Date: Tue, 18 Jun 2013 20:01:08 -0700 Subject: [PATCH] Port middleware error handler from ceilometer API Change-Id: I159923338e0447347d1c4bed4a80586ad6c06fcc --- ironic/api/app.py | 3 +- ironic/api/middleware.py | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 ironic/api/middleware.py diff --git a/ironic/api/app.py b/ironic/api/app.py index 23a288e316..2dea2e716a 100644 --- a/ironic/api/app.py +++ b/ironic/api/app.py @@ -22,6 +22,7 @@ import pecan from ironic.api import acl from ironic.api import config from ironic.api import hooks +from ironic.api import middleware auth_opts = [ cfg.StrOpt('auth_strategy', @@ -55,7 +56,6 @@ def setup_app(pecan_config=None, extra_hooks=None): pecan.configuration.set_config(dict(pecan_config), overwrite=True) -# TODO(deva): add middleware.ParsableErrorMiddleware from Ceilometer app = pecan.make_app( pecan_config.app.root, static_root=pecan_config.app.static_root, @@ -63,6 +63,7 @@ def setup_app(pecan_config=None, extra_hooks=None): debug=CONF.debug, force_canonical=getattr(pecan_config.app, 'force_canonical', True), hooks=app_hooks, + wrap_app=middleware.ParsableErrorMiddleware, ) if pecan_config.app.enable_acl: diff --git a/ironic/api/middleware.py b/ironic/api/middleware.py new file mode 100644 index 0000000000..1359fc55d3 --- /dev/null +++ b/ironic/api/middleware.py @@ -0,0 +1,91 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2012 New Dream Network, LLC (DreamHost) +# +# Author: Doug Hellmann +# +# 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. +""" +Middleware to replace the plain text message body of an error +response with one formatted so the client can parse it. + +Based on pecan.middleware.errordocument +""" + +import json +import webob +from xml import etree as et + +from ironic.openstack.common import log + +LOG = log.getLogger(__name__) + + +class ParsableErrorMiddleware(object): + """Replace error body with something the client can parse. + """ + def __init__(self, app): + self.app = app + + def __call__(self, environ, start_response): + # Request for this state, modified by replace_start_response() + # and used when an error is being reported. + state = {} + + def replacement_start_response(status, headers, exc_info=None): + """Overrides the default response to make errors parsable. + """ + try: + status_code = int(status.split(' ')[0]) + state['status_code'] = status_code + except (ValueError, TypeError): # pragma: nocover + raise Exception(( + 'ErrorDocumentMiddleware received an invalid ' + 'status %s' % status + )) + else: + if (state['status_code'] / 100) not in (2, 3): + # Remove some headers so we can replace them later + # when we have the full error message and can + # compute the length. + headers = [(h, v) + for (h, v) in headers + if h not in ('Content-Length', 'Content-Type') + ] + # Save the headers in case we need to modify them. + state['headers'] = headers + return start_response(status, headers, exc_info) + + app_iter = self.app(environ, replacement_start_response) + if (state['status_code'] / 100) not in (2, 3): + req = webob.Request(environ) + if (req.accept.best_match(['application/json', 'application/xml']) + == 'application/xml'): + try: + # simple check xml is valid + body = [et.ElementTree.tostring( + et.ElementTree.fromstring('' + + '\n'.join(app_iter) + + ''))] + except et.ElementTree.ParseError as err: + LOG.error('Error parsing HTTP response: %s' % err) + body = ['%s' % state['status_code'] + + ''] + state['headers'].append(('Content-Type', 'application/xml')) + else: + body = [json.dumps({'error_message': '\n'.join(app_iter)})] + state['headers'].append(('Content-Type', 'application/json')) + state['headers'].append(('Content-Length', len(body[0]))) + else: + body = app_iter + return body