返回 BadRequestError 实例时引发圣杯 "BadRequestError is not JSON serializable" 异常

Chalice "BadRequestError is not JSON serializable" exception raised when returning a BadRequestError instance

使用 Chalice BadRequestResponse class 进行视图异常处理导致异常表明 BadRequestResponse 不可 JSON 序列化。当从视图返回 BadRequestResponse 时,为什么 Chalice 试图将视图输出转换为 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)

哎呀!我正在尝试 return 而不是引发 BadRequestError 导致 Chalice 将异常转换为 JSON.