from rest_framework.response import Response from rest_framework.views import exception_handler def agentfeed_exception_handler(exc, context): """DRF exception handler that additionally maps pathological-input crashes to a clean 400 instead of an unhandled 500. Deeply nested JSON in the request body raises RecursionError inside DRF's JSONParser (before any view validation runs). RecursionError subclasses RuntimeError, not ValueError, so DRF's default handler doesn't map it and it surfaces as a 500. Anything the default handler already knows about is left to it; anything still unhandled that isn't a RecursionError is returned as None so Django produces its normal 500. """ response = exception_handler(exc, context) if response is not None: return response if isinstance(exc, RecursionError): return Response( {"detail": "Request payload is nested too deeply to parse."}, status=400, ) return None