Skip to content

Commit 15f85d6

Browse files
committed
Fix support of get_full_details() for Throttled exceptions
Since `str` objects are immutable, appending to existing `str` creates in fact a new `str` instance. Thus `ErrorDetail.detail.code` attribute is lost after `str` concatenation operation.
1 parent eafc9a2 commit 15f85d6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

rest_framework/exceptions.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,15 @@ class Throttled(APIException):
210210
default_code = 'throttled'
211211

212212
def __init__(self, wait=None, detail=None, code=None):
213-
super(Throttled, self).__init__(detail, code)
214213

215214
if wait is None:
216215
self.wait = None
217216
else:
218217
self.wait = math.ceil(wait)
219-
self.detail += ' ' + force_text(ungettext(
220-
self.extra_detail_singular.format(wait=self.wait),
221-
self.extra_detail_plural.format(wait=self.wait),
222-
self.wait
223-
))
218+
if detail is None:
219+
detail = ' '.join((
220+
force_text(self.default_detail),
221+
force_text(ungettext(self.extra_detail_singular.format(wait=self.wait),
222+
self.extra_detail_plural.format(wait=self.wait),
223+
self.wait))))
224+
super(Throttled, self).__init__(detail, code)

tests/test_exceptions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from __future__ import unicode_literals
22

33
from django.test import TestCase
4+
from django.utils import six
45
from django.utils.translation import ugettext_lazy as _
56

6-
from rest_framework.exceptions import ErrorDetail, _get_error_details
7+
from rest_framework.exceptions import (
8+
ErrorDetail, Throttled, _get_error_details
9+
)
710

811

912
class ExceptionTestCase(TestCase):
@@ -39,3 +42,13 @@ def test_get_error_details(self):
3942
_get_error_details([[lazy_example]])[0][0],
4043
ErrorDetail
4144
)
45+
46+
def test_get_full_details_with_throttling(self):
47+
exception = Throttled()
48+
assert exception.get_full_details() == {
49+
'message': 'Request was throttled.', 'code': 'throttled'}
50+
51+
exception = Throttled(wait=2)
52+
assert exception.get_full_details() == {
53+
'message': 'Request was throttled. Expected available in {} seconds.'.format(2 if six.PY3 else 2.),
54+
'code': 'throttled'}

0 commit comments

Comments
 (0)
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