python - 返回BadRequestError实例时引发酒杯 “BadRequestError is not JSON serializable”异常

标签 python amazon-web-services chalice

使用Chalice BadRequestResponse类进行 View 异常处理会导致异常,指出BadRequestResponse不可JSON序列化。为什么从 View 返回BadRequestResponse时,Chalice尝试将 View 输出转换为JSON?

@auth.route('/auth/register', methods=['POST'])
def login():
    user_data = auth.current_request.json_body
    try:
        UserSchema().load(user_data)
        user = User(**user_data)
        user.save()
    except ValidationError as e:
        return BadRequestError("Why! This shouldn't be serialized to JSON!")
    else:
        return Response(status_code=201, body=user_data)

最佳答案

哎呀!我试图返回而不是引发BadChainError,这导致Chalice将异常转换为JSON。

关于python - 返回BadRequestError实例时引发酒杯 “BadRequestError is not JSON serializable”异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63333940/

相关文章:

python - 如何将 pyspark 数据帧分成两行

java - 如何使用 aws cli 创建 java aws lambda 函数?

amazon-web-services - 与 AWS 上的无服务器框架一样,使用 terraform 创建隔离堆栈

linux - 无法获得运行 WordPress 的 EC2 Linux 的权限

python - chalice 框架 : Request did not specify an Accept header with image/jpeg

python - 如何在 python 中创建具有重复键的嵌套字典

python - 将修改后的状态传递给外部嵌套装饰器

python - 无法导入模块 'app' : No module named 'app' in Aws Lambda using Chalice

python - 查找每行具有最大值的列名

python - 在 Chalice 应用程序中使用 boto3 启动 EC2 实例不起作用