def __call__(self, req): serializer = serializers.JSONResponseSerializer() resp = webob.Response(request=req) default_webob_exc = webob.exc.HTTPInternalServerError() resp.status_code = self.error.get('code', default_webob_exc.code) serializer.default(resp, self.error) return resp
def create_resource(options): """Policy resource factory method.""" deserializer = wsgi.JSONRequestDeserializer() serializer = serializers.JSONResponseSerializer() return wsgi.Resource(PolicyController(options), deserializer, serializer)
def __call__(self, request): """WSGI method that controls (de)serialization and method dispatch.""" action_args = self.get_action_args(request.environ) action = action_args.pop('action', None) status_code = action_args.pop('success', None) try: deserialized_request = self.dispatch(self.deserializer, action, request) action_args.update(deserialized_request) LOG.debug(('Calling %(controller)s : %(action)s'), { 'controller': self.controller, 'action': action }) action_result = self.dispatch(self.controller, action, request, **action_args) except TypeError as err: LOG.error(_LE('Exception handling resource: %s') % err) msg = _('The server could not comply with the request since ' 'it is either malformed or otherwise incorrect.') err = webob.exc.HTTPBadRequest(msg) http_exc = translate_exception(err, request.best_match_language()) # NOTE(luisg): We disguise HTTP exceptions, otherwise they will be # treated by wsgi as responses ready to be sent back and they # won't make it into the pipeline app that serializes errors raise exception.HTTPExceptionDisguise(http_exc) except webob.exc.HTTPException as err: if not isinstance(err, webob.exc.HTTPError): # Some HTTPException are actually not errors, they are # responses ready to be sent back to the users, so we don't # create error log, but disguise and translate them to meet # openstacksdk's need. http_exc = translate_exception(err, request.best_match_language()) raise exception.HTTPExceptionDisguise(http_exc) if isinstance(err, webob.exc.HTTPServerError): LOG.error(_LE("Returning %(code)s to user: %(explanation)s"), { 'code': err.code, 'explanation': err.explanation }) http_exc = translate_exception(err, request.best_match_language()) raise exception.HTTPExceptionDisguise(http_exc) except exception.BileanException as err: raise translate_exception(err, request.best_match_language()) except Exception as err: log_exception(err, sys.exc_info()) raise translate_exception(err, request.best_match_language()) serializer = self.serializer or serializers.JSONResponseSerializer() try: response = webob.Response(request=request) # Customize status code if default (200) should be overridden if status_code is not None: response.status_code = int(status_code) # Customize 'location' header if provided if action_result and isinstance(action_result, dict): location = action_result.pop('location', None) if location: response.location = '/v1%s' % location if not action_result: action_result = None self.dispatch(serializer, action, response, action_result) return response # return unserializable result (typically an exception) except Exception: return action_result