# Path: django_sonic_screwdriver/models.py
# class ErrorCodes(Enum):
#     def __str__(self):
#         return str(self.value)


from django.utils.translation import ugettext_lazy as _
from rest_framework import exceptions, status
from rest_framework.views import set_rollback, Response
from .models import ErrorCodes



def handler(exception, context):
    data = dict()

    # 401 Handler
    if isinstance(exception, exceptions.NotAuthenticated):
        exception = UnauthorizedException()

    # 429 Handler
    if isinstance(exception, exceptions.Throttled):
        exception = TooManyRequestsException()

    if isinstance(exception, exceptions.ValidationError):
        # Convert the inconsistent Django error format into a consistent one.
        all_errors = []
        for key, err_details in exception.get_full_details().items():
            for ed in err_details:
                all_errors.append(
                    {
                        "message": ed["message"],
                        "code": ed["code"].value

# Path: django_sonic_screwdriver/models.py
# class ErrorCodes(Enum):
#     def __str__(self):
#         return str(self.value)


# Path: django_sonic_screwdriver/exceptions.py
from django.utils.translation import ugettext_lazy as _
from rest_framework import exceptions, status
from rest_framework.views import set_rollback, Response
from .models import ErrorCodes




def handler(exception, context):
    data = dict()

    # 401 Handler
    if isinstance(exception, exceptions.NotAuthenticated):
        exception = UnauthorizedException()

    # 429 Handler
    if isinstance(exception, exceptions.Throttled):
        exception = TooManyRequestsException()

    if isinstance(exception, exceptions.ValidationError):
        # Convert the inconsistent Django error format into a consistent one.
        all_errors = []
        for key, err_details in exception.get_full_details().items():
            for ed in err_details:
                all_errors.append(
                    {
                        "message": ed["message"],
                        "code": ed["code"].value

                        if isinstance(ed["code"], ErrorCodes)