wsw*_*wld 6 python exception httpexception werkzeug flask
Flask 400exception(abort())的默认消息是:
{
"message": "The browser (or proxy) sent a request that this server could not understand."
}
Run Code Online (Sandbox Code Playgroud)
用于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/ ?"
}
Run Code Online (Sandbox Code Playgroud)
当我将它们作为我的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
})
Run Code Online (Sandbox Code Playgroud)
对于400它的情况很好.但是,当涉及到404它仍然是相同的消息.我测试了它在我的代码在同一个地方-它的工作原理为abort(400),abort(403)和其他一些人,但这样做的神不知鬼不觉通过默认消息覆盖abort(404).调试没有多大帮助.这可能是罪魁祸首?
更新.是的,我使用的abort进口flask没有flask_restful因为后者不具有映射,这是一个函数不是一个Aborter对象.此外,它确实适用于大多数例外情况,因此这可能不是真正的问题.
更新2.执行时abort.mapping似乎完全没问题.有问题的例外被覆盖,包括404.

更新3:我已经整理了一个用于调试的小沙盒.(因为神秘的问题已经解决了,因此删除了回购).
| 归档时间: |
|
| 查看次数: |
2503 次 |
| 最近记录: |