示例#1
0
def exception_handler(exc):
    """
    Returns the response that should be used for any given exception.

    By default we handle the REST framework `APIException`, and also
    Django's builtin `Http404` and `PermissionDenied` exceptions.

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

    if isinstance(exc, exceptions.APIException):
        headers = {}
        if getattr(exc, "auth_header", None):
            headers["WWW-Authenticate"] = exc.auth_header
        if getattr(exc, "wait", None):
            headers["X-Throttle-Wait-Seconds"] = "%d" % exc.wait

        detail = format_exception(exc)
        return response.Response(detail,
                                 status=exc.status_code,
                                 headers=headers)

    elif isinstance(exc, Http404):
        return response.NotFound({'_error_message': str(exc)})

    elif isinstance(exc, DjangoPermissionDenied):
        return response.Forbidden({"_error_message": str(exc)})

    # Note: Unhandled exceptions will raise a 500 error.
    return None
示例#2
0
 def get(self, request, format=None):
     ret = {}
     for key, url_name in api_root_dict.items():
         try:
             ret[key] = reverse(url_name, request=request, format=format)
         except NoReverseMatch:
             # Support resources that are prefixed by a parametrized url
             ret[key] = request.build_absolute_uri() + key
     return response.Response(ret)
示例#3
0
 def list(self, request, *args, **kwargs):
     return response.Response(HOMEPAGE_CHOICES)