Skip to content
Snippets Groups Projects
Commit ca53974e authored by Patrick Jentsch's avatar Patrick Jentsch
Browse files

Update the generic error handling again. Added type hints

parent 5c2225c4
No related branches found
No related tags found
No related merge requests found
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
from .handlers import generic_error_handler from .handlers import generic
def init_app(app): def init_app(app):
app.register_error_handler(HTTPException, generic_error_handler) app.register_error_handler(HTTPException, generic)
from flask import jsonify, render_template, request, Response from flask import jsonify, render_template, request, Response
from werkzeug.exceptions import HTTPException 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 accent_json: bool = request.accept_mimetypes.accept_json
accept_html: bool = request.accept_mimetypes.accept_html accept_html: bool = request.accept_mimetypes.accept_html
if accent_json and not accept_html: if accent_json and not accept_html:
response: Response = jsonify(str(error)) response: Response = jsonify(str(error))
response.status_code = error.code return response, error.code
return response
return render_template('errors/error.html.j2', error=error), error.code return render_template('errors/error.html.j2', error=error), error.code
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment