def __call__(self, req): """Generate a WSGI response based on the exception passed to ctor.""" # Replace the body with fault details. code = self.wrapped_exc.status_int explanation = self.wrapped_exc.explanation LOG.debug("Returning %(code)s to user: %(explanation)s", { 'code': code, 'explanation': explanation }) def_ct = "text/plain" content_type = req.get_best_match_content_type(default_match=def_ct) mtype = serializers.get_media_map().get(content_type, "text") serializer = serializers.get_default_serializers()[mtype] env = {} serialized_exc = serializer(env).serialize(self.wrapped_exc) self.wrapped_exc.content_type = content_type self.wrapped_exc.body = serialized_exc[1] # We need to specify the HEAD req.method here to be HEAD because of the # way that webob.exc.WSGIHTTPException.__call__ generates the response. # The text/occi will not have a body since it is based on headers. We # cannot set this earlier in the middleware, since we are calling # OpenStack and it will fail because the responses won't contain a # body. if content_type == "text/occi": req.method = "HEAD" return self.wrapped_exc
def __call__(self, req): """Generate a WSGI response based on the exception passed to ctor.""" # Replace the body with fault details. code = self.wrapped_exc.status_int explanation = self.wrapped_exc.explanation LOG.debug("Returning %(code)s to user: %(explanation)s", {'code': code, 'explanation': explanation}) def_ct = "text/plain" content_type = req.get_best_match_content_type(default_match=def_ct) mtype = serializers.get_media_map().get(content_type, "text") serializer = serializers.get_default_serializers()[mtype] env = {} serialized_exc = serializer(env).serialize(self.wrapped_exc) self.wrapped_exc.content_type = content_type self.wrapped_exc.body = serialized_exc[1] # We need to specify the HEAD req.method here to be HEAD because of the # way that webob.exc.WSGIHTTPException.__call__ generates the response. # The text/occi will not have a body since it is based on headers. We # cannot set this earlier in the middleware, since we are calling # OpenStack and it will fail because the responses won't contain a # body. if content_type == "text/occi": req.method = "HEAD" return self.wrapped_exc
def get_serializer(self, content_type, default_serializers=None): """Returns the serializer for the wrapped object. Returns the serializer for the wrapped object subject to the indicated content type. If no serializer matching the content type is attached, an appropriate serializer drawn from the default serializers will be used. If no appropriate serializer is available, raises InvalidContentType. """ default_serializers = default_serializers or {} try: if content_type is None: content_type = "text/plain" mtype = serializers.get_media_map().get(content_type, content_type) if mtype in self.serializers: return mtype, self.serializers[mtype] else: return mtype, default_serializers[mtype] except (KeyError, TypeError): raise exception.InvalidContentType(content_type=content_type)