From 5c2225c43eda3b94f611e97664f0c6508b910363 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Mon, 13 Mar 2023 08:20:09 +0100
Subject: [PATCH] Let the generic error handler generate json again
---
app/errors/handlers.py | 13 ++++++++++---
app/templates/errors/error.html.j2 | 2 +-
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/app/errors/handlers.py b/app/errors/handlers.py
index 8c2e4fcf..bf2fef09 100644
--- a/app/errors/handlers.py
+++ b/app/errors/handlers.py
@@ -1,5 +1,12 @@
-from flask import render_template
+from flask import jsonify, render_template, request, Response
+from werkzeug.exceptions import HTTPException
-def generic_error_handler(e):
- return render_template('errors/error.html.j2', error=e), e.code
+def generic_error_handler(error: HTTPException):
+ accent_json: bool = request.accept_mimetypes.accept_json
+ accept_html: bool = request.accept_mimetypes.accept_html
+ if accent_json and not accept_html:
+ response: Response = jsonify(str(error))
+ response.status_code = error.code
+ return response
+ return render_template('errors/error.html.j2', error=error), error.code
diff --git a/app/templates/errors/error.html.j2 b/app/templates/errors/error.html.j2
index ef19de5f..8db723d2 100644
--- a/app/templates/errors/error.html.j2
+++ b/app/templates/errors/error.html.j2
@@ -4,7 +4,7 @@
{% block page_content %}
-
{{ error.name }}
+
{{ error.code }} {{ error.name }}
{{ error.description }}
{% endblock page_content %}