2023-05-15 10:00:13 +00:00
|
|
|
from flask import jsonify, render_template, request
|
2023-03-13 07:20:09 +00:00
|
|
|
from werkzeug.exceptions import HTTPException
|
2023-05-15 10:00:13 +00:00
|
|
|
from . import bp
|
2020-10-12 11:26:35 +00:00
|
|
|
|
|
|
|
|
2023-05-15 10:00:13 +00:00
|
|
|
@bp.app_errorhandler(HTTPException)
|
2024-12-12 09:32:08 +00:00
|
|
|
def handle_http_exception(e: HTTPException):
|
2023-05-15 10:00:13 +00:00
|
|
|
''' Generic HTTP exception handler '''
|
|
|
|
accept_json = request.accept_mimetypes.accept_json
|
|
|
|
accept_html = request.accept_mimetypes.accept_html
|
2024-12-12 09:32:08 +00:00
|
|
|
|
2023-05-15 10:00:13 +00:00
|
|
|
if accept_json and not accept_html:
|
2024-12-12 09:32:08 +00:00
|
|
|
error = {
|
|
|
|
'code': e.code,
|
|
|
|
'name': e.name,
|
|
|
|
'description': e.description
|
|
|
|
}
|
|
|
|
return jsonify(error), e.code
|
|
|
|
|
|
|
|
return render_template('errors/error.html.j2', error=e), e.code
|