In the Flask-restful documentation on custom error handlers it says:
Flask-RESTful will call the handle_error() function on any 400 or 500 error that happens on a Flask-RESTful route, and leave other routes alone. You may want your app to return an error message with the correct media type on 404 Not Found errors; in which case, use the catch_all_404s parameter of the Api constructor.
My simplified setup for an API with blueprint:
from flask import Blueprint
from flask_restful import Api
from .resources.tool import ToolDetail, Tools
api_bp = Blueprint('api', __name__, subdomain='<hostname>', url_prefix='/api')
api = Api(api_bp, catch_all_404s=True)
api.add_resource(Tools, '/tools', '/tools/<int:page>', '/tools/<int:page>/<int:per_page>')
api.add_resource(ToolDetail, '/tool/<int:id>')
With catch_all_404s=True
any 404 results in a response like:
{
"message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."
}
Also for requests on other blueprints! I would expect the catch_all_404s=True
to have an effect on the defined blueprint only.
Removing the catch_all_404s=True
from the snippet above the handle_error() function is never called. Also not for requests with the /api
path.
So my question is: how to make flask-restful catch all 404s within the blueprint, but not outside it (i.e. not in other blueprints).
Edit: as pointed out in the comment below, this catch_all behavior not being limited to the blueprint is caused by a limitation in Flask itself.
Still remaining problem: with catch_all_404s=False
, 404s are never caught by flask-restful, even within the routes, e.g. http://somehost.domain.com/api/tools/x is caught by the error handler of the app, not the error handler of flask-restful.