Flask custom error page 500 not working
10,014
Something I didn't realize... from the Flask docs
Please note that if you add an error handler for “500 Internal Server Error”, Flask will not trigger it if it’s running in Debug mode.
Author by
Dan Safee
Updated on July 27, 2022Comments
-
Dan Safee almost 2 years
I have the following code in
__init__.py
@app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.errorhandler(500) def internal_server_error(e): return render_template('500.html'), 500 @app.errorhandler(403) def page_forbidden(e): return render_template('403.html'), 500
It used to catch all 500 errors and show my nice 500.html template. However I moved all my views into separate blueprint files and now the 500 errorhandler does not work. It is only that handler though. 404 works just fine.
If the server throws a 500 error, it will display the default Chrome INTERNAL SERVER ERROR message and not my template. Did I do something wrong when I created all my blueprints that would create this issue?
Here is the entire
__init__.py
fileimport datetime import mysql.connector import os from flask import Flask, render_template, session, request, Blueprint from flask.ext.moment import Moment from flask.ext.login import LoginManager from db_classes import User from info import info_blueprint from claims import claims_blueprint from users import users_blueprint from members import members_blueprint from drug import drug_blueprint from auth import auth_blueprint from formulary import formulary_blueprint from config import MYSQL_USR, MYSQL_HOST, MYSQL_PASS, MYSQL_DB, MYSQL_PORT, second_to_live from decorators import role_required app = Flask(__name__, template_folder="static/templates") app.config.from_object('config') moment = Moment(app) login_manager = LoginManager() login_manager.init_app(app) login_manager.session_protection = 'strong' login_manager.login_view = 'login' @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) #################### # Blueprints #################### app.register_blueprint(info_blueprint) app.register_blueprint(claims_blueprint) app.register_blueprint(users_blueprint) app.register_blueprint(members_blueprint) app.register_blueprint(drug_blueprint) app.register_blueprint(formulary_blueprint) app.register_blueprint(auth_blueprint) ##################### # Error Routes ##################### @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.errorhandler(500) def internal_server_error(e): return render_template('500.html'), 500 @app.errorhandler(403) def page_forbidden(e): return render_template('403.html'), 500 ##################### # Context Processors ##################### @app.before_request def make_session_permanent(): session.permanent = True app.permanent_session_lifetime = datetime.timedelta(seconds=second_to_live) @app.context_processor def inject_time(): return dict(current_time=datetime.datetime.utcnow()) if __name__ == "__main__": app.run(host= '0.0.0.0', debug=True)
-
Paul Becotte over 8 yearsIs that
__init__.py
getting imported by your application entry point, preferably when you create your app? -
Dan Safee over 8 yearsit's the file i use to start the application so I suppose so. The create app code is just a few lines above the errorhandlers. Like I said it was working until i moved all my views to a separate file via blueprints. I posted the entire file above.
-
-
Dan Safee over 8 yearsYes that solved the issue. I had previously been running the app with
if __name__ == "__main__": app.run(host= '0.0.0.0', debug=True)
. When you do this through gunicorn it doesn't use that debug value. When I refactored to use a config file, gunicorn pulled the debug=True from the config file and the 500 template stopped working. Thank you for this information. -
code_dredd almost 5 yearsLink is broken. Please update when you get a chance.
-
Paul Becotte about 4 yearsLooks like this is no longer actually the case in Flask 1.1, at least, I can no longer find any references to this behavior. Flask has changed a LOT since the pre 1.0 releases, especially around the CLI and entrypoints.
-
Artemis over 3 yearsStill experiencing this in 1.1.2.
-
Lewis Morris almost 2 yearsyes, of course!!! I see now, thanks.