Skip to content

Fix support of get_full_details() for Throttled exceptions #4627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions rest_framework/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ class Throttled(APIException):
default_code = 'throttled'

def __init__(self, wait=None, detail=None, code=None):
if detail is None:
detail = force_text(self.default_detail)
if wait is not None:
wait = math.ceil(wait)
detail = ' '.join((
detail,
force_text(ungettext(self.extra_detail_singular.format(wait=wait),
self.extra_detail_plural.format(wait=wait),
wait))))
self.wait = wait
super(Throttled, self).__init__(detail, code)

if wait is None:
self.wait = None
else:
self.wait = math.ceil(wait)
self.detail += ' ' + force_text(ungettext(
self.extra_detail_singular.format(wait=self.wait),
self.extra_detail_plural.format(wait=self.wait),
self.wait
))
20 changes: 19 additions & 1 deletion tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import unicode_literals

from django.test import TestCase
from django.utils import six
from django.utils.translation import ugettext_lazy as _

from rest_framework.exceptions import ErrorDetail, _get_error_details
from rest_framework.exceptions import (
ErrorDetail, Throttled, _get_error_details
)


class ExceptionTestCase(TestCase):
Expand Down Expand Up @@ -39,3 +42,18 @@ def test_get_error_details(self):
_get_error_details([[lazy_example]])[0][0],
ErrorDetail
)

def test_get_full_details_with_throttling(self):
exception = Throttled()
assert exception.get_full_details() == {
'message': 'Request was throttled.', 'code': 'throttled'}

exception = Throttled(wait=2)
assert exception.get_full_details() == {
'message': 'Request was throttled. Expected available in {} seconds.'.format(2 if six.PY3 else 2.),
'code': 'throttled'}

exception = Throttled(wait=2, detail='Slow down!')
assert exception.get_full_details() == {
'message': 'Slow down! Expected available in {} seconds.'.format(2 if six.PY3 else 2.),
'code': 'throttled'}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy