Flask400
异常(abort()
)的默认消息为:
{
"message": "The browser (or proxy) sent a request that this server could not understand."
}
对于404
:
{
"message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. You have requested this URI [/obj/] but did you mean /obj/ or /obj/<int:id>/ or /obj/<int:id>/kill/ ?"
}
当我在API中得到这些消息作为回复时,我很难理解这些消息(尤其是第一个消息,我认为加密或标头出了点问题),我觉得尝试为每个abort()
异常手动覆盖文本有点累。因此,我更改了映射:
from flask import abort
from werkzeug.exceptions import HTTPException
class BadRequest(HTTPException):
code = 400
description = 'Bad request.'
class NotFound(HTTPException):
code = 404
description = 'Resource not found.'
abort.mapping.update({
400: BadRequest,
404: NotFound
})
对于400
它的情况下,效果很好。但是,谈到404
它,仍然是相同的信息。我测试了它在我的代码在同一个地方-它的工作原理为abort(400)
,abort(403)
和其他一些人,但这样做的神不知鬼不觉通过默认消息覆盖abort(404)
。调试没有太大帮助。这里的罪魁祸首是什么?
更新。是的,我使用的不是abort
从导入,因为后者没有映射,并且它是一个函数,而不是对象。此外,它确实适用于大多数例外情况,因此这可能不是真正的问题。flask
flask_restful
Aborter
更新2。在abort.mapping
似乎是执行完美的罚款。有问题的异常被覆盖,包括404
。
更新3 :我整理了一个小沙箱,用于调试。 (由于长期未解之谜,已删除了回购协议)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句