Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions rest_framework/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
(`django.http.Http404` and `django.core.exceptions.PermissionDenied`)
"""
import math
import warnings

from django.http import JsonResponse
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext

from rest_framework import status
from rest_framework import RemovedInDRF317Warning, status
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList


Expand Down Expand Up @@ -226,23 +227,31 @@ def __init__(self, media_type, detail=None, code=None):
class Throttled(APIException):
status_code = status.HTTP_429_TOO_MANY_REQUESTS
default_detail = _('Request was throttled.')
extra_detail_singular = _('Expected available in {wait} second.')
extra_detail_plural = _('Expected available in {wait} seconds.')
default_code = 'throttled'

def __init__(self, wait=None, detail=None, code=None):
if detail is None:
detail = force_str(self.default_detail)
if wait is not None:
wait = math.ceil(wait)
detail = ' '.join((
detail,
force_str(ngettext(self.extra_detail_singular.format(wait=wait),
self.extra_detail_plural.format(wait=wait),
wait))))
detail = " ".join((detail, self.extra_detail(wait)))
self.wait = wait
super().__init__(detail, code)

def extra_detail(self, wait):
if hasattr(self, 'extra_detail_singular') or hasattr(self, 'extra_detail_plural'):
warnings.warn("You're using incorrect way of specifying extra_detail, please override `extra_detail` instead. This will be removed in DRF 3.17", RemovedInDRF317Warning)
return ngettext(
getattr(self, 'extra_detail_singular', 'Expected available in {wait} second.'),
getattr(self, 'extra_detail_plural', 'Expected available in {wait} seconds.'),
wait,
).format(wait=wait)
return ngettext(
'Expected available in {wait} second.',
'Expected available in {wait} seconds.',
wait,
).format(wait=wait)


def server_error(request, *args, **kwargs):
"""
Expand Down
Loading