1# Copyright (c) 2013 NEC Corporation 2# All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may 5# not use this file except in compliance with the License. You may obtain 6# a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations 14# under the License. 15 16import logging 17import re 18 19import webob.dec 20import webob.exc 21 22from oslo_middleware import base 23 24 25LOG = logging.getLogger(__name__) 26 27_TOKEN_RE = re.compile(r'^(X-\w+-Token):.*$', flags=re.MULTILINE) 28 29 30class CatchErrors(base.ConfigurableMiddleware): 31 """Middleware that provides high-level error handling. 32 33 It catches all exceptions from subsequent applications in WSGI pipeline 34 to hide internal errors from API response. 35 """ 36 37 @webob.dec.wsgify 38 def __call__(self, req): 39 try: 40 response = req.get_response(self.application) 41 except Exception: 42 req_str = _TOKEN_RE.sub(r'\1: *****', req.as_text()) 43 LOG.exception('An error occurred during ' 44 'processing the request: %s', req_str) 45 response = webob.exc.HTTPInternalServerError() 46 return response 47