【发布时间】:2018-09-10 05:26:25
【问题描述】:
我有一个烧瓶应用程序,它有两种类型的路线:
(1)。网站路径,例如 /home、/user/、/news_feed
(2)。 json 返回移动应用的 api,如 /api/user、/api/weather 等。
我通过flask提供的@app.errorhandler装饰器使用自定义错误页面来处理404和500等常见错误 - 用于我的网站
@app_instance.errorhandler(404)
def page_note_found_error(err):
return render_template("err_404.html"), 404
@app_instance.errorhandler(500)
def internal_server_error(err):
db_instance.session.rollback()
return render_template("err_500.html"), 500
如果说我通过移动 API 收到 500 错误,我不希望我的移动 API 返回这些错误页面。
有没有办法绕过或自定义某些路由(api)的错误处理程序,以便它返回 json 响应而不是我的自定义错误页面
【问题讨论】:
标签: python flask error-handling custom-error-handling