File tree Expand file tree Collapse file tree 2 files changed +29
-11
lines changed Expand file tree Collapse file tree 2 files changed +29
-11
lines changed Original file line number Diff line number Diff line change @@ -210,14 +210,14 @@ class Throttled(APIException):
210
210
default_code = 'throttled'
211
211
212
212
def __init__ (self , wait = None , detail = None , code = None ):
213
+ if detail is None :
214
+ detail = force_text (self .default_detail )
215
+ if wait is not None :
216
+ wait = math .ceil (wait )
217
+ detail = ' ' .join ((
218
+ detail ,
219
+ force_text (ungettext (self .extra_detail_singular .format (wait = wait ),
220
+ self .extra_detail_plural .format (wait = wait ),
221
+ wait ))))
222
+ self .wait = wait
213
223
super (Throttled , self ).__init__ (detail , code )
214
-
215
- if wait is None :
216
- self .wait = None
217
- else :
218
- 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
- ))
Original file line number Diff line number Diff line change 1
1
from __future__ import unicode_literals
2
2
3
3
from django .test import TestCase
4
+ from django .utils import six
4
5
from django .utils .translation import ugettext_lazy as _
5
6
6
- from rest_framework .exceptions import ErrorDetail , _get_error_details
7
+ from rest_framework .exceptions import (
8
+ ErrorDetail , Throttled , _get_error_details
9
+ )
7
10
8
11
9
12
class ExceptionTestCase (TestCase ):
@@ -39,3 +42,18 @@ def test_get_error_details(self):
39
42
_get_error_details ([[lazy_example ]])[0 ][0 ],
40
43
ErrorDetail
41
44
)
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' }
55
+
56
+ exception = Throttled (wait = 2 , detail = 'Slow down!' )
57
+ assert exception .get_full_details () == {
58
+ 'message' : 'Slow down! Expected available in {} seconds.' .format (2 if six .PY3 else 2. ),
59
+ 'code' : 'throttled' }
You can’t perform that action at this time.
0 commit comments