The default message for Flask 400
exception (abort()
) is:
{
"message": "The browser (or proxy) sent a request that this server could not understand."
}
For 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/ ?"
}
I have trouble comprehending these messages when I'm getting them as replies in my API (especially the first one, I thought there's something wrong with encryption or headers) and I thing it's kinda tiresome to try to override text manually for every abort()
exception. So I change the mapping:
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
})
For the case of 400
it works beautifully. But when it comes to 404
it is still the same message. I tested it in the same place in my code - it works for abort(400)
, abort(403)
and some of the others, but it gets mysteriously overridden by default message on abort(404)
. Debugging didn't help much. What may be the culprit here?
Update. Yes, I'm using abort
imported from flask
not flask_restful
as the latter doesn't have the mapping and it's a function not an Aborter
object. Besides, it does work for most exceptions, so it's probably not the real issue here.
Update 2. The abort.mapping
seems to be perfectly fine on execution. The exceptions in question are overridden, including 404
.
Update 3: I've put together a little sandbox, that I use for debugging. (removed the repo since the mystery is long solved).
abort.mapping
because that attribute is not a public API.abort
is documented as a function, and with Werkzeug 0.12 this code actually doesn't work anymore. We just got a bugreport that implies that we broke our public API here.