import re import sys from collections.abc import Mapping from typing import Any from flask import Blueprint, Flask, current_app, got_request_exception from flask_restx import Api from werkzeug.exceptions import HTTPException from werkzeug.http import HTTP_STATUS_CODES from configs import dify_config from core.errors.error import AppInvokeQuotaExceededError def http_status_message(code): return HTTP_STATUS_CODES.get(code, "") def register_external_error_handlers(api: Api): @api.errorhandler(HTTPException) def handle_http_exception(e: HTTPException): got_request_exception.send(current_app, exception=e) # If Werkzeug already prepared a Response, just use it. if getattr(e, "response", None) is not None: return e.response status_code = getattr(e, "code", 500) or 500 # Build a safe, dict-like payload default_data = { "code": re.sub(r"(?