在Flask-Restful中,当某个特定异常被引发时,可以定义特定的静态消息:
errors = {
'SomeException': {
'message': 'Some custom error message',
'status': 500,
}
}
api_bp = Blueprint('my_bp', __name__)
api = Api(api_bp, errors=errors)问题是我的SomeException包含了我也想在响应中返回的数据。有什么办法吗?使用纯Flask,我只需创建一个包含所有所需逻辑的函数,并使用errorhandler装饰器对其进行装饰。
但是我如何使用Flask-Restful做到这一点呢?
发布于 2015-09-25 05:45:01
您可以在abort()调用中定义错误消息。
if something_is_wrong:
custom_message = 'Here is my custom error'
abort(400, message='There is an error: ' + custom_message)这将中止脚本并返回HTTP status 400响应,正文中包含以下内容:
{"message":"There is an error: Here is my custom error"}https://stackoverflow.com/questions/32771178
复制相似问题