mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 04:12:45 +00:00 
			
		
		
		
	A better application structure
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user