Pyramid 捕获异常日志 Exception log


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application."""

    config = Configurator(settings=settings)

    config.add_forbidden_view(http_response.forbidden, renderer="json")
    config.add_notfound_view(http_response.not_found, renderer="json")
    
    # 添加异常处理view
    config.add_exception_view(http_response.failed_validation, renderer="json")


# 异常处理函数
def failed_validation(exc, request):
    response = request.response
    if 'jws_token' in request.cookies and request.cookies['jws_token']:
        response.set_cookie('jws_token', request.cookies['jws_token'], max_age=timedelta(hours=1))

    else:
        response.delete_cookie('jws_token')

    if type(exc) == errors.CustomException:
        return responseFail_dict(request, error=exc.args[0])

    elif type(exc) == errors.RequestJsonException:
        return responseFail_dict(request, error=(errors.INVALID_REQUEST_BODY_JSON[0],
                                                 errors.INVALID_REQUEST_BODY_JSON[1].format(error=str(exc))))
    else:
        # 记录异常信息 logger.exception('Failed to open file')  
        logging.error('='*8+str(exc)+'='*8, exc_info=True)

    return responseFail_dict(request, error=(errors.UNKNOW_ERROR[0], str(exc)))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值