Skip to content

Commit

Permalink
feat: custom 400 error message
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Oct 11, 2023
1 parent a87a973 commit dd5a1c9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
21 changes: 21 additions & 0 deletions web/flaskr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ def global_processor():
}


def setup_error_pages(app):
from flask import render_template

@app.errorhandler(400)
def bad_request(error):
return render_template("errors/400.html", error=error), 400

@app.errorhandler(403)
def not_authorized(error):
return render_template("errors/403.html", error=error), 403

@app.errorhandler(404)
def not_found(error):
return render_template("errors/404.html", error=error), 404

@app.errorhandler(500)
def internal_error(error):
return render_template("errors/500.html", error=error), 500


def create_app(test_config=None, gunicorn_logging=False):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
Expand All @@ -93,6 +113,7 @@ def create_app(test_config=None, gunicorn_logging=False):
setup_csrf(app)
setup_database(app)
setup_jinja(app)
setup_error_pages(app)

# ensure the instance folder exists
os.makedirs(app.instance_path, exist_ok=True)
Expand Down
30 changes: 0 additions & 30 deletions web/flaskr/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,33 +1544,3 @@ def delete_video_meeting():
@auth.oidc_logout
def logout():
return redirect(url_for("routes.index"))


@current_app.errorhandler(403)
def page_not_authorized(e):
return (
render_template(
"errors/403.html",
),
403,
)


@current_app.errorhandler(404)
def page_not_found(e):
return (
render_template(
"errors/404.html",
),
404,
)


@current_app.errorhandler(500)
def page_error(e):
return (
render_template(
"errors/500.html",
),
500,
)
18 changes: 18 additions & 0 deletions web/flaskr/templates/errors/400.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'layout.html' %}

{% block main %}
<div class="fr-container">
<div class="fr-grid-row fr-grid-row--center">
<div class="fr-col-md-6">
<h1 class="fr-h2">{% trans %}Erreur 400{% endtrans %}</h1>
<p>
{% if error %}
{{ error }}
{% else %}
{% trans %}La requête que vous avez effectué est invalide. Vous pouvez <a href="/">retourner à l’accueil</a>.{% endtrans %}
{% endif %}
</p>
</div>
</div>
</div>
{% endblock %}
34 changes: 34 additions & 0 deletions web/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
from flask import abort
from flask import url_for


def test_custom_400(client_app):
@client_app.app.route("/custom_400")
def custom_400():
abort(400, "custom error message")

response = client_app.get("/custom_400", status=400)
response.mustcontain("Erreur 400")
response.mustcontain("custom error message")


def test_custom_404(client_app):
response = client_app.get("/invalid-url", status=404)
response.mustcontain("Erreur 404")


def test_custom_403(client_app):
@client_app.app.route("/custom_403")
def custom_403():
abort(403)

response = client_app.get("/custom_403", status=403)
response.mustcontain("Erreur 403")


def test_custom_500(client_app):
@client_app.app.route("/custom_500")
def custom_500():
abort(500)

response = client_app.get("/custom_500", status=500)
response.mustcontain("Erreur 500")


def test_root__anonymous_user(client_app):
response = client_app.get("/", status=302)

Expand Down

0 comments on commit dd5a1c9

Please sign in to comment.