使用Flask-Restful引发自定义错误 [英] Raising A Custom Error with Flask-Restful

查看:89
本文介绍了使用Flask-Restful引发自定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,我正在尝试使用Flask-Restful引发自定义错误,并遵循

这似乎是标准的409错误,没有任何自定义;从文档中,我希望收到自定义错误消息-具有该用户名的用户已存在."

我认为我缺少有关错误本身引发的信息.我应该以任何方式使用字典键吗?尽管我尝试了一下,但查看Flask-Restful的源代码并没有帮助.

解决方案

要使用 Flask-RESTful 为标准 HTTP状态代码定义一条消息,您必须重新定义一个 Werkzeug 提供的HTTP异常中, Flask 基于. >

以下是您忽略问题的示例,该示例覆盖了 例外:

errors = {
    'Conflict': {
        'message': "A user with that username already exists.",
        'status': 409,
    },
}

app = Flask(__name__)
api = flask_restful.Api(app, errors=errors)

因此,每次调用abort(409)时,这将返回正确的媒体类型和定义的消息的表示形式.

但是,通过使用此方法,在任何地方您都将使用409状态代码中止,这将返回有关用户名已经存在的用户的消息.在处理除用户以外的其他资源的视图中调用abort(409)时,这不太可能实现.

因此,我建议您每次要提供自定义消息时,只需使用 Flask-RESTful abort方法:

from flask.ext.restful import abort

abort(409, description="A user with that username already exists.")

通常来说,当您引发不在raise()而不是abort())时,通过定义自定义错误消息来扩展Flask-RESTful非常有用. .org/docs/0.10/exceptions/#error-classes"rel =" noreferrer>由Werkzeug提供的HTTP异常.

All, I'm trying to raise a custom error using Flask-Restful, following the docs. For testing purposes, I've defined and registered the errors dictionary exactly link in the docs: api = flask_restful.Api(app, errors=errors).

However, when I want to raise the custom error using (e.g.) abort(409) within the resource module, firebug reports:

{ "message": "Conflict", "status": 409 }

This seems like the standard 409 error, nothing custom; from the docs, I would expect the custom error message- "A user with that username already exists."

I think I'm missing something regarding the raising of the error itself. Should I use the dictionary key in any way? Reviewing the Flask-Restful source code didn't help, though I tried.

解决方案

To define a message for a standard HTTP status code with Flask-RESTful, you must redefine one of the HTTP exceptions provided by Werkzeug, on which Flask is based.

Following your question, here is an example to override the Conflict exception:

errors = {
    'Conflict': {
        'message': "A user with that username already exists.",
        'status': 409,
    },
}

app = Flask(__name__)
api = flask_restful.Api(app, errors=errors)

Hence, every time you will call abort(409), this will return a representation in the right mediatype, and with the defined message.

However, by using this method, in any place you will abort with a 409 status code, this will return a message about a user with a username that already exists. This is unlikely what you want when you call abort(409) in a view that deals with other resources than users.

So, I advise you to simply use the abort method of Flask-RESTful as follows, every time you want to provide a custom message:

from flask.ext.restful import abort

abort(409, description="A user with that username already exists.")

Generally speaking, extending Flask-RESTful by defining custom error messages is useful when you raise custom exceptions (with raise(), not abort()) which are not in the HTTP exceptions provided by Werkzeug.

这篇关于使用Flask-Restful引发自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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