from rest_framework.views import exception_handler
from rest_framework.response import Response


def custom_exception_handler(exc, context):
    """
    Handler global de excepciones para DRF.
    Añadir en settings.py:
        REST_FRAMEWORK = {
            'EXCEPTION_HANDLER': 'general.exception.custom_exception_handler',
        }
    """
    response = exception_handler(exc, context)

    if response is not None:
        response.data = {
            'body': {'data': [], 'error': response.data},
            'mensaje': str(exc),
        }

    return response
