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)))