在Flask中捕获500个服务器错误 [英] Catching a 500 server error in Flask

查看:193
本文介绍了在Flask中捕获500个服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢Flask的错误捕捉。它非常简单:

pre $ app $ errorhandler
def pageNotFound(error):
return 找不到页面

像魅力一样。但它不适用于500错误代码。我想捕捉Python错误,当出现错误的代码中引发异常。这是可能的吗?



我应该注意,如果我明确地在视图中调用 return abort(500),那么500错误处理程序工作。所以这是Python代码失败时显式的。



这可能吗?

div>

默认情况下,你所描述的是Flask是如何工作的。我的假设是你在调试模式下运行,因此在调试屏幕上显示异常。确保调试模式关闭,然后重试。这里是直接从代码本身发表评论:


当发生
未被捕获的异常时,默认的异常处理将被触发。在调试模式下,异常将立即重新产生
,否则会被记录下来,并使用500内部
服务器错误的处理程序。如果不存在这样的处理程序,则会显示默认的500
内部服务器错误消息。



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:

Default exception handling that kicks in when an exception occurs that is not caught. In debug mode the exception will be re-raised immediately, otherwise it is logged and the handler for a 500 internal server error is used. If no such handler exists, a default 500 internal server error message is displayed.

这篇关于在Flask中捕获500个服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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