Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erreurs 400 personnalisées #37

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 59 additions & 25 deletions web/flaskr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,17 @@
LANGUAGES = ["en", "fr"]


def setup_babel(app):
babel = Babel(app)

@babel.localeselector
def get_locale():
if request.args.get("lang"):
session["lang"] = request.args["lang"]
return session.get("lang", "fr")


def create_app(test_config=None, gunicorn_logging=False):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
def setup_cache(app):
cache.init_app(
app,
config={
"CACHE_TYPE": "flask_caching.backends.filesystem",
"CACHE_DIR": "/tmp/flask-caching",
},
)


def setup_logging(app, test_config=None, gunicorn_logging=False):
if gunicorn_logging:
gunicorn_logger = logging.getLogger("gunicorn.error")
app.logger.handlers = gunicorn_logger.handlers
Expand All @@ -53,6 +44,34 @@ def create_app(test_config=None, gunicorn_logging=False):
if test_config:
app.config.from_mapping(test_config)


def setup_i18n(app):
babel = Babel(app)

@babel.localeselector
def get_locale():
if request.args.get("lang"):
session["lang"] = request.args["lang"]
return session.get("lang", "fr")


def setup_csrf(app):
csrf = CSRFProtect()
csrf.init_app(app)


def setup_database(app):
with app.app_context():
import flaskr.routes

app.register_blueprint(flaskr.routes.bp)
from .models import db

db.init_app(app)
Migrate(app, db, compare_type=True)


def setup_jinja(app):
@app.context_processor
def global_processor():
return {
Expand All @@ -64,22 +83,37 @@ def global_processor():
**app.config["WORDINGS"],
}

# translations
setup_babel(app)

# Protect App Form with CSRF
csrf = CSRFProtect()
csrf.init_app(app)
def setup_error_pages(app):
from flask import render_template

# init database
with app.app_context():
import flaskr.routes
@app.errorhandler(400)
def bad_request(error):
return render_template("errors/400.html", error=error), 400

app.register_blueprint(flaskr.routes.bp)
from .models import db
@app.errorhandler(403)
def not_authorized(error):
return render_template("errors/403.html", error=error), 403

db.init_app(app)
Migrate(app, db, compare_type=True)
@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)
setup_cache(app)
setup_logging(app, test_config, gunicorn_logging)
setup_i18n(app)
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