python - 猎鹰引发自定义异常json
问题描述
当我加注时,falcon.HTTPNotFound
我会关注 JSON
{
"title": "404 Not Found",
"description": {
"message": "Model mymodel does not exist!",
"model_id": "mymodel"
}
}
但我想在下面发送而不是这个
{
"message": "Model mymodel does not exist!",
"model_id": "mymodel"
}
我如何在 Falcon 中实现这一目标?
def expect_model_existence(req, resp, resource, params, require_exists):
model_id = params['model_id']
if require_exists and not Model.exists(model_id=model_id):
response = {"message": f"Model {model_id} does not exist!", "model_id": model_id}
raise falcon.HTTPNotFound(description=response)
elif not require_exists and Model.exists(model_id=model_id):
response = {"message": f"Model {model_id} already exists!", "model_id": model_id}
raise falcon.HTTPConflict(description=response)
定义了一个函数来检查模型的存在并像以前一样应用钩子
@falcon.before(expect_model_existence, True)
def on_delete(self, req, resp, model_id):
pass
解决方案
推荐阅读
- angular - 如何为 router.events.forEach 编写 jasmine.createSpyObject(...)
- mysql - TypeORM:尝试手动运行迁移时“没有待处理的迁移”
- django - Django 表单没有被翻译
- javascript - 我该怎么做才能使我的添加到购物车按钮起作用?
- ios - Swift UI 测试:无法安装请求的应用程序
- unity3d - 我已经实现了水平滑动以将玩家旋转 90 度,但是旋转相机会破坏我的滑动逻辑。我该如何解决这个问题?
- python - (python 初学者)有没有其他方法可以根据用户输入将 str() 转换为 int() 和 float() ?
- python - 如何根据先前索引处新创建的值执行计算?
- eclipse-hono - 无法在 Hono 中使用基于 X.509 的身份验证注册租户
- scala - 为在 Scala 中并行运行 Databricks 笔记本的未来序列添加重试