"""Plugin error isolation wrapper.""" import logging import functools from fastapi import UploadFile as _UploadFile # noqa: F401 — needed for ForwardRef resolution from fastapi.responses import JSONResponse logger = logging.getLogger(__name__) def wrap_plugin_route(handler): """Decorator that isolates plugin route errors and returns structured JSON. Does NOT use functools.wraps to avoid copying __annotations__ and __wrapped__ — FastAPI would otherwise try to resolve ``ForwardRef('UploadFile')`` from the original handler's signature. """ async def wrapper(*args, **kwargs): try: return await handler(*args, **kwargs) except Exception as exc: logger.error(f'Plugin route error: {exc}', exc_info=True) return JSONResponse( status_code=500, content={'detail': f'Plugin error: {exc}', 'code': 'plugin_error'} ) # Preserve identity for debugging but NOT __wrapped__ or __annotations__ wrapper.__name__ = getattr(handler, '__name__', 'wrapper') wrapper.__module__ = getattr(handler, '__module__', __name__) wrapper.__qualname__ = getattr(handler, '__qualname__', 'wrapper') return wrapper