"Fossies" - the Fresh Open Source Software Archive

Member "karbor-1.3.0/karbor/api/middleware/fault.py" (26 Mar 2019, 2348 Bytes) of package /linux/misc/openstack/karbor-1.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "fault.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.0_vs_1.3.0.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 from oslo_log import log as logging
   14 import six
   15 from six.moves import http_client
   16 import webob.dec
   17 import webob.exc
   18 
   19 from karbor.api.openstack import wsgi
   20 from karbor import exception
   21 from karbor import utils
   22 from karbor.wsgi import common as base_wsgi
   23 
   24 
   25 LOG = logging.getLogger(__name__)
   26 
   27 
   28 class FaultWrapper(base_wsgi.Middleware):
   29     """Calls down the middleware stack, making exceptions into faults."""
   30 
   31     _status_to_type = {}
   32 
   33     @staticmethod
   34     def status_to_type(status):
   35         if not FaultWrapper._status_to_type:
   36             for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError):
   37                 FaultWrapper._status_to_type[clazz.code] = clazz
   38         return FaultWrapper._status_to_type.get(
   39             status, webob.exc.HTTPInternalServerError)()
   40 
   41     def _error(self, inner, req):
   42         LOG.error('Middleware error occurred: %s', inner.message)
   43         safe = getattr(inner, 'safe', False)
   44         headers = getattr(inner, 'headers', None)
   45         status = getattr(inner, 'code', http_client.INTERNAL_SERVER_ERROR)
   46         if status is None:
   47             status = http_client.INTERNAL_SERVER_ERROR
   48 
   49         msg_dict = dict(url=req.url, status=status)
   50         LOG.info("%(url)s returned with HTTP %(status)d", msg_dict)
   51         outer = self.status_to_type(status)
   52         if headers:
   53             outer.headers = headers
   54 
   55         if safe:
   56             msg = (inner.msg if isinstance(inner, exception.KarborException)
   57                    else six.text_type(inner))
   58             outer.explanation = msg
   59         return wsgi.Fault(outer)
   60 
   61     @webob.dec.wsgify(RequestClass=wsgi.Request)
   62     def __call__(self, req):
   63         try:
   64             return req.get_response(self.application)
   65         except Exception as ex:
   66             return self._error(ex, req)