import re import sys from typing import Any from flask import current_app, got_request_exception from flask_restx import Api from werkzeug.datastructures import Headers from werkzeug.exceptions import HTTPException from werkzeug.http import HTTP_STATUS_CODES from core.errors.error import AppInvokeQuotaExceededError def http_status_message(code): """Maps an HTTP status code to the textual status""" return HTTP_STATUS_CODES.get(code, "") def register_external_error_handlers(api: Api) -> None: """Register error handlers for the API using decorators. :param api: The Flask-RestX Api instance """ @api.errorhandler(HTTPException) def handle_http_exception(e: HTTPException): """Handle HTTP exceptions.""" got_request_exception.send(current_app, exception=e) if e.response is not None: return e.get_response() headers = Headers() status_code = e.code default_data = { "code": re.sub(r"(?