当DEBUG为False时错误处理程序不触发 [英] errorhandler not firing when DEBUG is 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)
当DEBUG
为True
时,此方法正确运行,这也将PROPAGATE_EXCEPTIONS
隐式设置为True
.但是,当DEBUG
是False
时,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 errorhandler
s. 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屋!