Skip to content Skip to sidebar Skip to footer

Catch An Exception And Displaying A Custom Error Page

I have a Flask app. I made a custom exception in one of my libraries related to a very specific error that I would like to notify the user about. What I would like to happen is wh

Solution 1:

You need to register an error handler for the exception. The error handler behaves like a normal view, it should return a response (or response-like data). In this case, I'm assuming you want to ultimately send a 500 status code, so that's why there's a , 500 along with the return. The handler receives the exception instance as the first argument, so you can use that when rendering a template if it has special information.

class SpecificException(Exception):
    pass

@app.errorhandler(SpecificException)
def handle_specific_exception(e):
    return render_template('errors/specific_exception.html', e=e), 500

Post a Comment for "Catch An Exception And Displaying A Custom Error Page"