refactor: set Retry-After header directly

This commit is contained in:
Saqib Ansari 2026-04-10 22:43:45 +05:30
parent 2f30dac5d8
commit 76eb3297cd
4 changed files with 4 additions and 19 deletions

View file

@ -394,12 +394,6 @@ def handle_exception(e):
elif http_status_code == 429:
response = frappe.rate_limiter.respond()
elif http_status_code == 503:
retry_after = getattr(e, "retry_after", 10)
response = frappe.utils.response.report_error(503)
if response:
response.headers["Retry-After"] = str(retry_after)
else:
response = ErrorPage(
http_status_code=http_status_code, title=_("Server Error"), message=_("Uncaught Exception")

View file

@ -72,9 +72,9 @@ def concurrent_limit(limit: int | None = None, wait_timeout: int | None = None):
if not acquired:
from frappe.exceptions import ServiceUnavailableError
retry_after = max(1, int(effective_wait))
exc = ServiceUnavailableError(frappe._("Server is busy. Please try again in a few seconds."))
exc.retry_after = retry_after
retry_after = max(1, int(effective_wait))
frappe.local.response_headers.set("Retry-After", str(retry_after))
raise exc
try:

View file

@ -87,14 +87,7 @@ class TooManyRequestsError(Exception):
class ServiceUnavailableError(Exception):
"""Raised when a concurrency limit is exceeded for an endpoint.
Set :attr:`retry_after` (seconds) before raising so that the response
includes a ``Retry-After`` header.
"""
http_status_code = 503
retry_after: int = 10
class ImproperDBConfigurationError(Exception):

View file

@ -91,9 +91,8 @@ class TestConcurrentLimit(IntegrationTestCase):
del frappe.local.request
frappe.cache.delete(key)
def test_service_unavailable_has_correct_http_status_and_retry_after(self):
"""The raised exception must carry http_status_code=503 and retry_after
equal to the configured wait_timeout."""
def test_service_unavailable_has_correct_http_status(self):
"""The raised exception must carry http_status_code=503."""
TIMEOUT = 1
@concurrent_limit(limit=1, wait_timeout=TIMEOUT)
@ -110,7 +109,6 @@ class TestConcurrentLimit(IntegrationTestCase):
fn()
exc = ctx.exception
self.assertEqual(exc.http_status_code, 503)
self.assertEqual(exc.retry_after, TIMEOUT)
finally:
del frappe.local.request
frappe.cache.delete(key)