1
# Copyright 2010 United States Government as represented by the
2
# Administrator of the National Aeronautics and Space Administration.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
from oslo_log import log
22
from manila.api.openstack import wsgi
23
from manila.i18n import _LE
24
from manila.i18n import _LI
25
from manila import utils
26
from manila import wsgi as base_wsgi
28
LOG = log.getLogger(__name__)
31
class FaultWrapper(base_wsgi.Middleware):
32
"""Calls down the middleware stack, making exceptions into faults."""
37
def status_to_type(status):
38
if not FaultWrapper._status_to_type:
39
for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError):
40
FaultWrapper._status_to_type[clazz.code] = clazz
41
return FaultWrapper._status_to_type.get(
42
status, webob.exc.HTTPInternalServerError)()
44
def _error(self, inner, req):
45
LOG.exception(_LE("Caught error: %s"), six.text_type(inner))
47
safe = getattr(inner, 'safe', False)
48
headers = getattr(inner, 'headers', None)
49
status = getattr(inner, 'code', 500)
53
msg_dict = dict(url=req.url, status=status)
54
LOG.info(_LI("%(url)s returned with HTTP %(status)d"), msg_dict)
55
outer = self.status_to_type(status)
57
outer.headers = headers
58
# NOTE(johannes): We leave the explanation empty here on
59
# purpose. It could possibly have sensitive information
60
# that should not be returned back to the user. See
61
# bugs 868360 and 874472
62
# NOTE(eglynn): However, it would be over-conservative and
63
# inconsistent with the EC2 API to hide every exception,
64
# including those that are safe to expose, see bug 1021373
66
outer.explanation = '%s: %s' % (inner.__class__.__name__,
68
return wsgi.Fault(outer)
70
@webob.dec.wsgify(RequestClass=wsgi.Request)
71
def __call__(self, req):
73
return req.get_response(self.application)
74
except Exception as ex:
75
return self._error(ex, req)