Flask-restful not-found for blueprint

时间:2016-11-09 15:04:09

标签: python flask flask-restful

custom error handlers的Flask-restful文档中,它说:

  

Flask-RESTful将在Flask-RESTful路由上发生的任何400或500错误上调用handle_error()函数,并保留其他路由。您可能希望您的应用在404 Not Found错误上返回包含正确媒体类型的错误消息;在这种情况下,请使用Api构造函数的catch_all_404s参数。

我使用蓝图的API的简化设置:

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>')

使用catch_all_404s=True任何404会产生如下响应:

{
  "message": "The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again."
}

也适用于其他蓝图上的请求!我希望catch_all_404s=True仅对定义的蓝图产生影响。

永远不会调用从handle_error()函数上方的代码段中删除catch_all_404s=True。也不适用于/api路径的请求。

所以我的问题是:如何在蓝图中捕捉所有404s,但不在其他地方(即不在其他蓝图中)。

编辑:正如下面的评论中所指出的,此catch_all行为不仅限于蓝图,而是由Flask本身的限制引起的。 仍然存在的问题:使用catch_all_404s=False,即使在路线中,404也不会被烧瓶安息, http://somehost.domain.com/api/tools/x被app的错误处理程序捕获,而不是flask-restful的错误处理程序。

0 个答案:

没有答案