当DEBUG为False时错误处理程序不触发 [英] errorhandler not firing when DEBUG is False

查看:71
本文介绍了当DEBUG为False时错误处理程序不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用错误处理程序进行捕获和处理某些例外情况:

I'm using errorhandlers to catch and handle certain kinds of exceptions:

@app.errorhandler(CustomException)
def handle_custom_exception(error):
    return redirect('redirect-path', code=301)

DEBUGTrue时,此方法正确运行,这也将PROPAGATE_EXCEPTIONS隐式设置为True.但是,当DEBUGFalse时,PROPAGATE_EXCEPTIONS默认为False,并且Flask对于抛出的所有错误返回500,而忽略已注册的errorhandler.将PROPAGATE_EXCEPTIONS设置为True可以纠正这种情况下的错误处理.

This works correctly when DEBUG is True, which implicitly sets PROPAGATE_EXCEPTIONS to True as well. When DEBUG is False though, PROPAGATE_EXCEPTIONS defaults to False and Flask returns a 500 for all errors thrown, ignoring the registered errorhandlers. Setting PROPAGATE_EXCEPTIONS to True corrects the error handling in this case.

我想知道的是:

  • 在生产中启用PROPAGATE_EXCEPTIONS是否安全?我应该关注任何副作用吗?

  • Is it safe to have PROPAGATE_EXCEPTIONS enabled in production? Are there any side effects I should be concerned about?

为什么Flask在调试与非调试中此配置的默认值不同?

Why does Flask have different default values for this config in debug vs non-debug?

推荐答案

您应该添加 app.config['PROPAGATE_EXCEPTIONS'] = True 之所以发生这种情况,是因为flask会覆盖通常的错误处理代码(适用于其控制下的所有路由).

You should add app.config['PROPAGATE_EXCEPTIONS'] = True It happens because flask overrides the usual error handling code (for all routes under its control).

这篇关于当DEBUG为False时错误处理程序不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆