Skip to content Skip to sidebar Skip to footer

Flask And Sys.excepthook

I want to add global exception handling object to my Flask webproject. In main module, where application class is created I've added code to override sys.excepthook. Here is simple

Solution 1:

Flask already handles exceptions in views for you. Using sys.excepthook is not the right way to handle this, the hook will never be called.

Specify a custom exception handler with Flask instead, see the Redirects and Errors section of the Quickstart manual:

from flask import render_template

@app.errorhandler(500)defpage_not_found(error):
    return'some error message'

Flask also uses the logging module to record exceptions; configure the logging module to write anything of severity logging.ERROR to the console or a log file.

Since you use app.run(debug=True, port=5001), you'll already see exceptions printed to the console.

Note that the 500 error page is only ever invoked when debug is not set to True however; otherwise the Werkzeug debug view is invoked instead.

Post a Comment for "Flask And Sys.excepthook"