A better application structure

This commit is contained in:
Patrick Jentsch
2023-05-15 12:00:13 +02:00
parent 1c47d2a346
commit 60a59383c7
16 changed files with 215 additions and 228 deletions

View File

@ -1,6 +1,5 @@
from werkzeug.exceptions import HTTPException
from .handlers import generic
from flask import Blueprint
def init_app(app):
app.register_error_handler(HTTPException, generic)
bp = Blueprint('errors', __name__)
from . import handlers

View File

@ -1,13 +1,14 @@
from flask import jsonify, render_template, request, Response
from flask import jsonify, render_template, request
from werkzeug.exceptions import HTTPException
from typing import Tuple, Union
from . import bp
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))
@bp.app_errorhandler(HTTPException)
def handle_http_exception(error):
''' Generic HTTP exception handler '''
accept_json = request.accept_mimetypes.accept_json
accept_html = request.accept_mimetypes.accept_html
if accept_json and not accept_html:
response = jsonify(str(error))
return response, error.code
return render_template('errors/error.html.j2', error=error), error.code