19 lines
748 B
Python
19 lines
748 B
Python
"""HTTP handling for recoverable database errors."""
|
|
|
|
from django.http import JsonResponse
|
|
|
|
|
|
def handle_database_operational_error(request, error):
|
|
"""Return a retryable response when PostgreSQL rejects a lock wait."""
|
|
cause = error.__cause__
|
|
while cause is not None:
|
|
if getattr(cause, "sqlstate", None) == "55P03" or getattr(cause, "pgcode", None) == "55P03":
|
|
response = JsonResponse(
|
|
{"detail": "درخواست بهدلیل شلوغی موقت پایگاه داده انجام نشد؛ کمی بعد دوباره تلاش کنید."},
|
|
status=503,
|
|
)
|
|
response["Retry-After"] = "2"
|
|
return response
|
|
cause = cause.__cause__
|
|
raise error
|