Source code for ferrosoft.apps.ferrobase.api.exception

import logging
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler


[docs] def ferrobase_exception_handler(exc, context): """Custom DRF exception handler that catches all otherwise-unhandled exceptions. DRF's built-in handler only converts a small set of framework exceptions into API responses. Any other exception propagates as a 500 HTML error page. This handler intercepts those cases, logs them, and returns a generic ``APIException`` response so callers always receive a structured JSON body. Args: exc: The exception raised during request processing. context: DRF handler context dict (contains ``view`` and ``request``). Returns: Response: A DRF Response for the given exception, or a generic 500 response when the exception is not natively handled by DRF. """ response = exception_handler(exc, context) if response is not None: return response # Response is None if DRF doesn't handle the exception (it handles only # a tiny subset of exceptions). In this case, just log the error and # return generic error response. logging.error(str(exc)) return exception_handler(APIException(), context)