问题描述
我喜欢Flask的错误捕捉。它非常简单:pre $ app $ errorhandler
def pageNotFound(error):
return 找不到页面
像魅力一样。但它不适用于500错误代码。我想捕捉Python错误,当出现错误的代码中引发异常。这是可能的吗?
我应该注意,如果我明确地在视图中调用 return abort(500)
,那么500错误处理程序工作。所以这是Python代码失败时显式的。
这可能吗?
div>默认情况下,你所描述的是Flask是如何工作的。我的假设是你在调试模式下运行,因此在调试屏幕上显示异常。确保调试模式关闭,然后重试。这里是
I love Flask's error catching. It's beautifully simple:
@app.errorhandler(404)
def pageNotFound(error):
return "page not found"
works like charm. But it doesn't work for the 500 error code. I want to catch Python errors when something goes wrong an exception is raised in the code. Is that possible?
I should note that if I explicitly call return abort(500)
in a view then the 500 errorhandler does work. So this is explicitly for when the Python code fails.
Is this possible?
What you have described is, by default, how Flask works. My assumption is that you are running in debug mode, and therefore exceptions are being shown to you in the debug screen. Make sure debug mode is off, then try again. Here is a comment directly from the code itself:
这篇关于在Flask中捕获500个服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!