在Flask中捕获500服务器错误

时间:2021-07-15 20:29:57

I love Flask's error catching. It's beautifully simple:

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

@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?

像魅力一样工作。但它不适用于500错误代码。我想在出现问题时捕获Python错误,在代码中引发异常。那可能吗?

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.

我应该注意,如果我在视图中显式调用return abort(500),则500错误处理程序确实有效。因此,当Python代码失败时,这是明确的。

Is this possible?

这可能吗?

4 个解决方案

#1


27  

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的工作原理。我的假设是您在调试模式下运行,因此在调试屏幕中向您显示异常。确保调试模式已关闭,然后重试。以下是代码本身的注释:

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.

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

#2


19  

It works fine in my side:

它在我身边很好用:

from flask import Flask ,url_for,render_template,request,abort
from  werkzeug.debug import get_current_traceback
app = Flask(__name__)

@app.route('/')
def index():
    try:
        raise Exception("Can't connect to database")
    except Exception,e:
        track= get_current_traceback(skip=1, show_hidden_frames=True,
            ignore_system_exceptions=False)
        track.log()
        abort(500)
    return "index"

@app.errorhandler(500)
def internal_error(error):

    return "500 error"

@app.errorhandler(404)
def not_found(error):
    return "404 error",404

if __name__== "__main__":
    app.run(debug=True)

Flask will not set the error code for you, so make sure to also provide the HTTP status code when returning a response.

Flask不会为您设置错误代码,因此请确保在返回响应时也提供HTTP状态代码。

#3


13  

here is my code snippt

这是我的代码snippt

@app.route('/')
def index():
    raise Exception("Can't connect to database")


@app.errorhandler(Exception)
def exception_handler(error):
    return "!!!!"  + repr(error)

#4


9  

My solution to this was to turn on the propagation of exceptions, by modifying the config dictionary:

我的解决方案是通过修改配置字典来打开异常的传播:

app = Flask(__name__)
...
app.config['PROPAGATE_EXCEPTIONS'] = True

Look at this other related question: Flask app raises a 500 error with no exception

看看这个其他相关的问题:Flask应用程序引发了500错误,没有异常

#1


27  

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的工作原理。我的假设是您在调试模式下运行,因此在调试屏幕中向您显示异常。确保调试模式已关闭,然后重试。以下是代码本身的注释:

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.

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

#2


19  

It works fine in my side:

它在我身边很好用:

from flask import Flask ,url_for,render_template,request,abort
from  werkzeug.debug import get_current_traceback
app = Flask(__name__)

@app.route('/')
def index():
    try:
        raise Exception("Can't connect to database")
    except Exception,e:
        track= get_current_traceback(skip=1, show_hidden_frames=True,
            ignore_system_exceptions=False)
        track.log()
        abort(500)
    return "index"

@app.errorhandler(500)
def internal_error(error):

    return "500 error"

@app.errorhandler(404)
def not_found(error):
    return "404 error",404

if __name__== "__main__":
    app.run(debug=True)

Flask will not set the error code for you, so make sure to also provide the HTTP status code when returning a response.

Flask不会为您设置错误代码,因此请确保在返回响应时也提供HTTP状态代码。

#3


13  

here is my code snippt

这是我的代码snippt

@app.route('/')
def index():
    raise Exception("Can't connect to database")


@app.errorhandler(Exception)
def exception_handler(error):
    return "!!!!"  + repr(error)

#4


9  

My solution to this was to turn on the propagation of exceptions, by modifying the config dictionary:

我的解决方案是通过修改配置字典来打开异常的传播:

app = Flask(__name__)
...
app.config['PROPAGATE_EXCEPTIONS'] = True

Look at this other related question: Flask app raises a 500 error with no exception

看看这个其他相关的问题:Flask应用程序引发了500错误,没有异常