HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ExceptionHandlerMixin error mapping

Reston opened this issue · comments

As per the exception handler mixin

class ExceptionHandlerMixin:
    ...
    expected_exceptions = {
        ...
        PermissionError: rest_exceptions.PermissionDenied
    }

Shouldn't django.core.exceptions.PermissionDenied map to rest_exceptions.PermissionDenied?

By default, DRF's default exception handling mechanism handles the Django built-in Http404 and PermissionDenied exceptions:

def exception_handler(exc, context):
    """
    Returns the response that should be used for any given exception.
    By default we handle the REST framework `APIException`, and also
    Django's built-in `Http404` and `PermissionDenied` exceptions.

    Any unhandled exceptions may return `None`, which will cause a 500 error
    to be raised.
    """
    ...

As it is stated:

Any unhandled exceptions may return None, which will cause a 500 error to be raised.

This is where ExceptionHandlerMixin comes to the rescue:

def handle_exception(self, exc):
    if isinstance(exc, tuple(self.expected_exceptions.keys())):
        # Handles exceptions "which will cause a 500 error to be raised"
        drf_exception_class = self.expected_exceptions[exc.__class__]
        drf_exception = drf_exception_class(get_error_message(exc))

        return super().handle_exception(drf_exception)

    # Handles django.core.exceptions.<PermissionDenied | Http404>
    return super().handle_exception(exc)

Thus, there is no need of explicitly mapping these two specific exceptions as they are handled by default from Django REST Framework. You can read more here.

I think we can be more clear about the specific exceptions that we are handling, by providing the @wencakisa example & explanation 🤔

Resolved by #26

Thank you for opening the issue, @Reston.