Commit 775de5111b55c5c36d20691d8c66e64b7dbdcf6e
1 parent
9b19aa41
Exists in
master
and in
4 other branches
Error handlers for 500, 404 and 405
Showing
5 changed files
with
47 additions
and
0 deletions
Show diff stats
.gitignore
app/__init__.py
@@ -97,4 +97,7 @@ def create_app(config_class=None): | @@ -97,4 +97,7 @@ def create_app(config_class=None): | ||
97 | from .auth import bp as auth_bp | 97 | from .auth import bp as auth_bp |
98 | app.register_blueprint(auth_bp) | 98 | app.register_blueprint(auth_bp) |
99 | 99 | ||
100 | + from .errors import bp as errors_bp | ||
101 | + app.register_blueprint(errors_bp) | ||
102 | + | ||
100 | return app | 103 | return app |
@@ -0,0 +1,31 @@ | @@ -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 |