

from django.core.exceptions import ValidationError as DjangoValidationError

from rest_framework.exceptions import ValidationError as DRFValidationError
from rest_framework.views import exception_handler as drf_exception_handler

class DrfUtil:
    @staticmethod
    def exception_handler(exc, context):
        """Handle Django ValidationError as an accepted exception
        Must be set in settings:
        >>> REST_FRAMEWORK = {
        ...     # ...
        ...     'EXCEPTION_HANDLER': 'mtp.apps.common.drf.exception_handler',
        ...     # ...
        ... }
        For the parameters, see ``exception_handler``
        """

        if isinstance(exc, DjangoValidationError):
            exc = DRFValidationError(detail=exc.message_dict)

        return drf_exception_handler(exc, context)