diff --git a/zerver/middleware.py b/zerver/middleware.py index c23924f92b..c83465797f 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -334,8 +334,11 @@ class RateLimitMiddleware(object): def process_exception(self, request, exception): # type: (HttpRequest, Exception) -> HttpResponse if isinstance(exception, RateLimited): - resp = json_error(_("API usage exceeded rate limit, try again in %s secs") % ( - request._ratelimit_secs_to_freedom,), status=429) + resp = json_error( + _("API usage exceeded rate limit"), + data={'retry-after': request._ratelimit_secs_to_freedom}, + status=429 + ) resp['Retry-After'] = request._ratelimit_secs_to_freedom return resp diff --git a/zerver/tests/test_external.py b/zerver/tests/test_external.py index dbab2592ab..75145a508c 100644 --- a/zerver/tests/test_external.py +++ b/zerver/tests/test_external.py @@ -112,9 +112,9 @@ class RateLimitTests(ZulipTestCase): self.assertEqual(result.status_code, 429) json = ujson.loads(result.content) self.assertEqual(json.get("result"), "error") - self.assertIn("API usage exceeded rate limit, try again in", json.get("msg")) + self.assertIn("API usage exceeded rate limit", json.get("msg")) + self.assertEqual(json.get('retry-after'), 0.5) self.assertTrue('Retry-After' in result) - self.assertIn(result['Retry-After'], json.get("msg")) self.assertEqual(result['Retry-After'], '0.5') # We actually wait a second here, rather than force-clearing our history,