diff --git a/.gitignore b/.gitignore index 968cd29..5106488 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ _static _templates *db db_config.py +logs diff --git a/app/__init__.py b/app/__init__.py index 068b1f9..26cd063 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -97,4 +97,7 @@ def create_app(config_class=None): from .auth import bp as auth_bp app.register_blueprint(auth_bp) + from .errors import bp as errors_bp + app.register_blueprint(errors_bp) + return app diff --git a/app/errors/__init__.py b/app/errors/__init__.py new file mode 100644 index 0000000..cf35d52 --- /dev/null +++ b/app/errors/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +bp = Blueprint('errors', __name__, url_prefix='/auth', template_folder='templates') + +from . import handlers diff --git a/app/errors/handlers.py b/app/errors/handlers.py new file mode 100644 index 0000000..420cc81 --- /dev/null +++ b/app/errors/handlers.py @@ -0,0 +1,31 @@ +from flask import render_template +from app import db +from . import bp + + +# Inspired by: +# https://flask.palletsprojects.com/en/master/patterns/errorpages/ + +@bp.app_errorhandler(403) +def forbidden_error(error): + error_title = "Page Interdite" + return render_template('error.html', error_title=error_title, error_msg=error), 403 + + +@bp.app_errorhandler(404) +def not_found_error(error): + error_title = "Page Introuvable." + return render_template('error.html', error_title=error_title, error_msg=error), 404 + + +@bp.app_errorhandler(405) +def method_error(error): + error_title = "Erreur de Méthode." + return render_template('error.html', error_title=error_title, error_msg=error), 405 + + +@bp.app_errorhandler(500) +def internal_error(error): + db.session.rollback() + error_title = "Erreur Interne. Administrateur Prévenu." + return render_template('error.html', error_title=error_title, error_msg=error), 500 diff --git a/app/errors/templates/error.html b/app/errors/templates/error.html new file mode 100644 index 0000000..50a3a3c --- /dev/null +++ b/app/errors/templates/error.html @@ -0,0 +1,7 @@ +{% extends "base_page.html" %} + +{% block content %} +