问题
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)
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.
What i'm wondering about is:
Is it safe to have
PROPAGATE_EXCEPTIONS
enabled in production? Are there any side effects I should be concerned about?Why does Flask have different default values for this config in debug vs non-debug?
回答1:
You should add
app.config['PROPAGATE_EXCEPTIONS'] = True
It happens because flask overrides the usual error handling code (for all routes under its control).
来源:https://stackoverflow.com/questions/33423580/errorhandler-not-firing-when-debug-is-false