Files
leocrm/app/core/plugin_error_handler.py
T
Agent Zero 7968630840 fix: UploadFile ForwardRef + WebSocket 403 — root cause fixed
1. plugin_error_handler.py: Remove _UploadFile alias, import UploadFile directly
   so FastAPI can resolve ForwardRef('UploadFile') in the wrapper's namespace.
   Also import WebSocket for ForwardRef resolution.

2. main.py: Skip WebSocket routes in wrap_plugin_route — WebSocket endpoints
   must not be wrapped (different protocol, no JSONResponse on error)
2026-07-27 01:17:59 +02:00

46 lines
1.8 KiB
Python

"""Plugin error isolation wrapper."""
import logging
import functools
import inspect
from fastapi import UploadFile # noqa: F401 — needed for ForwardRef resolution
from fastapi import WebSocket # noqa: F401 — needed for ForwardRef resolution of WebSocket params
from fastapi.responses import JSONResponse
logger = logging.getLogger(__name__)
def wrap_plugin_route(handler):
"""Decorator that isolates plugin route errors and returns structured JSON.
Copies the original handler's signature so FastAPI sees the correct
parameters (path params, query params, body, etc.) instead of *args/**kwargs.
Removes return annotation to prevent ForwardRef('UploadFile') resolution issues.
"""
@functools.wraps(handler)
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'}
)
# Remove annotations that cause ForwardRef resolution issues
wrapper.__annotations__ = {}
# Remove __wrapped__ so FastAPI doesn't try to resolve the original signature
if hasattr(wrapper, '__wrapped__'):
delattr(wrapper, '__wrapped__')
# Copy the signature from the original handler but remove return annotation
try:
orig_sig = inspect.signature(handler)
# Create new signature without return annotation
new_params = list(orig_sig.parameters.values())
wrapper.__signature__ = orig_sig.replace(
parameters=new_params,
return_annotation=inspect.Signature.empty,
)
except (ValueError, TypeError):
pass
return wrapper