python类HTTP_500的实例源码

designs.py 文件源码 项目:drydock 作者: att-comdev 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def on_post(self, req, resp, design_id):
        ingester_name = req.params.get('ingester', None)

        if ingester_name is None:
            self.error(
                None,
                "DesignsPartsResource POST requires parameter 'ingester'")
            self.return_error(
                resp,
                falcon.HTTP_400,
                message="POST requires parameter 'ingester'",
                retry=False)
        else:
            try:
                raw_body = req.stream.read(req.content_length or 0)
                if raw_body is not None and len(raw_body) > 0:
                    parsed_items = self.ingester.ingest_data(
                        plugin_name=ingester_name,
                        design_state=self.state_manager,
                        content=raw_body,
                        design_id=design_id,
                        context=req.context)
                    resp.status = falcon.HTTP_201
                    resp.body = json.dumps(
                        [x.obj_to_simple() for x in parsed_items])
                else:
                    self.return_error(
                        resp,
                        falcon.HTTP_400,
                        message="Empty body not supported",
                        retry=False)
            except ValueError:
                self.return_error(
                    resp,
                    falcon.HTTP_500,
                    message="Error processing input",
                    retry=False)
            except LookupError:
                self.return_error(
                    resp,
                    falcon.HTTP_400,
                    message="Ingester %s not registered" % ingester_name,
                    retry=False)
policy.py 文件源码 项目:promenade 作者: att-comdev 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __call__(self, f):
        @functools.wraps(f)
        def secure_handler(slf, req, resp, *args, **kwargs):
            ctx = req.context
            policy_eng = ctx.policy_engine
            # policy engine must be configured
            if policy_eng is not None:
                LOG.debug(
                    'Enforcing policy %s on request %s using engine %s',
                    self.action,
                    ctx.request_id,
                    policy_eng.__class__.__name__,
                    ctx=ctx)
            else:
                LOG.error('No policy engine configured', ctx=ctx)
                raise ex.PromenadeException(
                    title="Auth is not being handled by any policy engine",
                    status=falcon.HTTP_500,
                    retry=False)

            authorized = False
            try:
                if policy_eng.authorize(self.action, ctx):
                    LOG.debug('Request is authorized', ctx=ctx)
                    authorized = True
            except Exception:
                LOG.exception(
                    'Error authorizing request for action %s',
                    self.action,
                    ctx=ctx)
                raise ex.ApiError(
                    title="Expectation Failed",
                    status=falcon.HTTP_417,
                    retry=False)

            if authorized:
                return f(slf, req, resp, *args, **kwargs)
            else:
                # raise the appropriate response exeception
                if ctx.authenticated:
                    LOG.error(
                        'Unauthorized access attempted for action %s',
                        self.action,
                        ctx=ctx)
                    raise ex.ApiError(
                        title="Forbidden",
                        status=falcon.HTTP_403,
                        description="Credentials do not permit access",
                        retry=False)
                else:
                    LOG.error(
                        'Unathenticated access attempted for action %s',
                        self.action,
                        ctx=ctx)
                    raise ex.ApiError(
                        title="Unauthenticated",
                        status=falcon.HTTP_401,
                        description="Credentials are not established",
                        retry=False)

        return secure_handler


问题


面经


文章

微信
公众号

扫码关注公众号