exceptions.py 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
"""
Custom exceptions, that allow details to be passed as dict values (which can be
converted to JSON, like other API responses.
"""

from rest_framework import exceptions


# TODO: Override Throttled, UnsupportedMediaType, ValidationError.  These types require
# more careful handling of arguments.


class _DictAPIException(exceptions.APIException):
    """
    Intermediate class to allow exceptions to pass dict detail values.  Use by
    subclassing this along with another subclass of `exceptions.APIException`.
    """
    def __init__(self, detail):
        if isinstance(detail, dict):
            self.detail = detail
        else:
            super(_DictAPIException, self).__init__(detail)


class AuthenticationFailed(exceptions.AuthenticationFailed, _DictAPIException):
    """
    Override of DRF's AuthenticationFailed exception to allow dictionary responses.
    """
    pass


class MethodNotAllowed(exceptions.MethodNotAllowed, _DictAPIException):
    """
    Override of DRF's MethodNotAllowed exception to allow dictionary responses.
    """
    def __init__(self, method, detail=None):
        if isinstance(detail, dict):
            self.detail = detail
        else:
            super(MethodNotAllowed, self).__init__(method, detail)


class NotAcceptable(exceptions.NotAcceptable, _DictAPIException):
    """
    Override of DRF's NotAcceptable exception to allow dictionary responses.
    """

    def __init__(self, detail=None, available_renderers=None):
        self.available_renderers = available_renderers
        if isinstance(detail, dict):
            self.detail = detail
        else:
            super(NotAcceptable, self).__init__(detail, available_renderers)


class NotAuthenticated(exceptions.NotAuthenticated, _DictAPIException):
    """
    Override of DRF's NotAuthenticated exception to allow dictionary responses.
    """
    pass


class NotFound(exceptions.NotFound, _DictAPIException):
    """
    Override of DRF's NotFound exception to allow dictionary responses.
    """
    pass


class ParseError(exceptions.ParseError, _DictAPIException):
    """
    Override of DRF's ParseError exception to allow dictionary responses.
    """
    pass


class PermissionDenied(exceptions.PermissionDenied, _DictAPIException):
    """
    Override of DRF's PermissionDenied exception to allow dictionary responses.
    """
    pass