Catch an exception and displaying a custom error page Catch an exception and displaying a custom error page flask flask

Catch an exception and displaying a custom error page


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