简体   繁体   English

如何使用 Connexion + Tornado 更改所有错误的错误格式

[英]How to change error format of all errors using Connexion + Tornado

I'm using Connexion ( https://github.com/zalando/connexion ) to make sure my openapi-specification is well-followed and to have easy integration points to connect my routes to the underlying functions.我正在使用 Connexion ( https://github.com/zalando/connexion ) 来确保我的 openapi 规范得到很好的遵循,并有简单的集成点将我的路由连接到底层功能。

In any case, the default error responses from Connexion are json responses following the Problem Details for HTTP APIs RFC.在任何情况下,来自 Connexion 的默认错误响应都是遵循HTTP API RFC 问题详细信息的json 响应。 That is the following format, eg:这是以下格式,例如:

{
    "detail": "None is not of type 'object'",
    "status": 404,
    "title": "BadRequest",
    "type": "about:blank"
}

However, I would like to change the format of all errors sent to something like:但是,我想将发送的所有错误的格式更改为:

{
    error: {
        code: 400,
        message: 'BadRequest',
        detail: 'ID unknown'
        innererror: {...}
    }
}

I can not find any way to intercept every error to change the format of what is returned.我找不到任何方法来拦截每个错误以更改返回内容的格式。 I know I can extend the connection.exception.ProblemException class and add a dict to the ext parameter in its constructor, but for any 400 error for example, I can't intercept it.我知道我可以扩展connection.exception.ProblemException类并向其构造函数中的ext参数添加一个 dict,但是例如,对于任何400错误,我无法拦截它。

So, I know that it is possible to add error handlers for specific error codes, eg:所以,我知道可以为特定的错误代码添加错误处理程序,例如:

app.add_error_handler(404, error.normalize)
app.add_error_handler(400, error.normalize)

However, for the 404 handler I manage to successfully intercept the error.但是,对于404处理程序,我设法成功拦截了错误。 But for the 400 (eg a json validation error) - the interception does not work.但是对于400 (例如 json 验证错误)-拦截不起作用。

How can I intercept each and every error that is sent from Connexion and change the json format, even if it is just to extend it like:我如何拦截从 Connexion 发送的每个错误并更改 json 格式,即使它只是扩展它,例如:

{
    "detail": "Could not find page",
    "error": {
        "code": 404,
        "message": "Could not find requested document."
    },
    "status": 404,
    "title": "NotFound",
    "type": "about:blank"
}

I use Connexion, with a 'tornado' server.我使用带有“龙卷风”服务器的 Connexion。

Thanks in advance.提前致谢。 Tom汤姆

With the latest version (connexion==2.5.1) this works for me:使用最新版本(connexion==2.5.1)这对我有用:

from connexion import ProblemException
[...]

connexion_app.add_error_handler(400, render_http_exception)
connexion_app.add_error_handler(404, render_http_exception)
connexion_app.add_error_handler(ProblemException, render_problem_exception)

My exception handling functions:我的异常处理功能:

from flask import jsonify


def render_http_exception(error):

    resp = {
        'error': {
            'status': error.name,
            'code': error.code,
            'message': error.description,
        }
    }

    return jsonify(resp), error.code


def render_problem_exception(error):

    resp = {
        'error': {
            'status': error.title,
            'code': error.status,
            'message': error.detail,
        }
    }

    return jsonify(resp), error.status

You can easily change it to your format.您可以轻松地将其更改为您的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM