Skip to content

Commit bbb5dc9

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 bbb5dc9

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

rest_framework/exceptions.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,11 @@ def __init__(self, wait=None, detail=None, code=None):
216216
self.wait = None
217217
else:
218218
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-
))
219+
code = self.detail.code
220+
self.detail = ErrorDetail(
221+
' '.join((self.detail, force_text(ungettext(
222+
self.extra_detail_singular.format(wait=self.wait),
223+
self.extra_detail_plural.format(wait=self.wait),
224+
self.wait
225+
)))))
226+
self.detail.code = 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