2026-07-26 12:49:39 +02:00
|
|
|
"""Plugin error isolation wrapper."""
|
|
|
|
|
import logging
|
|
|
|
|
import functools
|
2026-07-26 20:45:42 +02:00
|
|
|
from fastapi import UploadFile as _UploadFile # noqa: F401 — needed for ForwardRef resolution
|
2026-07-26 12:49:39 +02:00
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wrap_plugin_route(handler):
|
2026-07-26 20:45:42 +02:00
|
|
|
"""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.
|
|
|
|
|
"""
|
2026-07-26 12:49:39 +02:00
|
|
|
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'}
|
|
|
|
|
)
|
2026-07-26 20:45:42 +02:00
|
|
|
# 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')
|
2026-07-26 12:49:39 +02:00
|
|
|
return wrapper
|