Commit 775de5111b55c5c36d20691d8c66e64b7dbdcf6e

Authored by hitier
1 parent 9b19aa41

Error handlers for 500, 404 and 405

.gitignore
... ... @@ -11,3 +11,4 @@ _static
11 11 _templates
12 12 *db
13 13 db_config.py
  14 +logs
... ...
app/__init__.py
... ... @@ -97,4 +97,7 @@ def create_app(config_class=None):
97 97 from .auth import bp as auth_bp
98 98 app.register_blueprint(auth_bp)
99 99  
  100 + from .errors import bp as errors_bp
  101 + app.register_blueprint(errors_bp)
  102 +
100 103 return app
... ...
app/errors/__init__.py 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +from flask import Blueprint
  2 +
  3 +bp = Blueprint('errors', __name__, url_prefix='/auth', template_folder='templates')
  4 +
  5 +from . import handlers
... ...
app/errors/handlers.py 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +from flask import render_template
  2 +from app import db
  3 +from . import bp
  4 +
  5 +
  6 +# Inspired by:
  7 +# https://flask.palletsprojects.com/en/master/patterns/errorpages/
  8 +
  9 +@bp.app_errorhandler(403)
  10 +def forbidden_error(error):
  11 + error_title = "Page Interdite"
  12 + return render_template('error.html', error_title=error_title, error_msg=error), 403
  13 +
  14 +
  15 +@bp.app_errorhandler(404)
  16 +def not_found_error(error):
  17 + error_title = "Page Introuvable."
  18 + return render_template('error.html', error_title=error_title, error_msg=error), 404
  19 +
  20 +
  21 +@bp.app_errorhandler(405)
  22 +def method_error(error):
  23 + error_title = "Erreur de Méthode."
  24 + return render_template('error.html', error_title=error_title, error_msg=error), 405
  25 +
  26 +
  27 +@bp.app_errorhandler(500)
  28 +def internal_error(error):
  29 + db.session.rollback()
  30 + error_title = "Erreur Interne. Administrateur Prévenu."
  31 + return render_template('error.html', error_title=error_title, error_msg=error), 500
... ...
app/errors/templates/error.html 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +{% extends "base_page.html" %}
  2 +
  3 +{% block content %}
  4 +<h2 class="error_title">{{ error_title }}</h2>
  5 +<div class="error_msg">{{ error_msg }}</div>
  6 +<a href="{{url_for('main.index')}}">Retour à l'accueil</a>
  7 +{% endblock %}
... ...