From ca53974e504680f59a19a90b14fcb338a7772b12 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Mon, 13 Mar 2023 08:36:51 +0100 Subject: [PATCH] Update the generic error handling again. Added type hints --- app/errors/__init__.py | 4 ++-- app/errors/handlers.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/errors/__init__.py b/app/errors/__init__.py index 1f0480b4..847658fb 100644 --- a/app/errors/__init__.py +++ b/app/errors/__init__.py @@ -1,6 +1,6 @@ from werkzeug.exceptions import HTTPException -from .handlers import generic_error_handler +from .handlers import generic def init_app(app): - app.register_error_handler(HTTPException, generic_error_handler) + app.register_error_handler(HTTPException, generic) diff --git a/app/errors/handlers.py b/app/errors/handlers.py index bf2fef09..fe7aaf4f 100644 --- a/app/errors/handlers.py +++ b/app/errors/handlers.py @@ -1,12 +1,13 @@ from flask import jsonify, render_template, request, Response from werkzeug.exceptions import HTTPException +from typing import Tuple, Union -def generic_error_handler(error: HTTPException): +def generic(error: HTTPException) -> Tuple[Union[str, Response], int]: + ''' Generic error handler ''' 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 response, error.code return render_template('errors/error.html.j2', error=error), error.code