Python Connexion —控件“类型"输入400响应错误 [英] Python Connexion — Control "Type" Key in 400 Response Errors

查看:98
本文介绍了Python Connexion —控件“类型"输入400响应错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 connexion ,这是一个REST API的python库,带有一个粗略的定义.对于实际的请求,它可以正常工作,但是当出现错误情况(例如,验证失败)时,它将返回类似以下的响应:

I'm using connexion, a python library for REST API's, with a swagger definition. It's working properly for the actual requests, but when there is an error condition, such as validation fails, it returns a response like:

{
  "type": "about:blank",
  "title": "Bad Request",
  "status": 400,
  "detail": "None is not of type 'string'"
} 

标题,状态和详细信息都很好并且很有意义,但是我有办法控制 type 键的值,以便我可以提供更多有用的信息,而不是简单地使用 about:空白在那里?

The title, status and detail all are good and make sense, but is there a way for me to control the type key's value so that I can provide more helpful information rather than simply having about:blank in there?

在后台,连接似乎使用了请求和烧瓶,所以也许我可以利用它们吗?

Under the hood, it appears that connexion uses requests and flask, so maybe there is something I can leverage from them?

推荐答案

我从未使用过底层框架,但是通过快速扫描,该模块公开了Flask应用程序的构造函数.这样,您就可以将您的swagger文件定义为一个新应用

I have never worked with the underlying framework, but with a quick scan, the module exposes the Flask application constructor. With that, you can define a new app with your swagger file as

app = connexion.App(__name__, specification_dir='swagger/')

,然后添加自定义错误处理程序.例如,您可以执行400错误

and then add custom error handlers. For example for the 400 error you can do

from flask import jsonify

@app.errorhandler(400)
def page_not_found(e):
    custom_data = {
        'type': 'Advanced type'
        # etc
    }
    return jsonify(custom_data)

此处

这篇关于Python Connexion —控件“类型"输入400响应错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆