fix: preserve original status code in handle_exception_on_proxy (#23901)

Root cause: handle_exception_on_proxy always maps non-HTTPException/non-ProxyException errors to 500, even when the original exception carries a status_code (e.g. 429 for rate limits).

Made-with: Cursor
This commit is contained in:
Avik Kumar 2026-03-19 04:56:06 -04:00
parent e5baa2232f
commit 71bf9fe0bb
2 changed files with 31 additions and 2 deletions

View file

@ -5148,11 +5148,17 @@ def handle_exception_on_proxy(e: Exception) -> ProxyException:
)
elif isinstance(e, ProxyException):
return e
original_status_code = getattr(e, "status_code", None)
if isinstance(original_status_code, int) and 400 <= original_status_code < 600:
error_code = original_status_code
else:
error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return ProxyException(
message="Internal Server Error, " + str(e),
message=getattr(e, "message", str(e)),
type=ProxyErrorTypes.internal_server_error,
param=getattr(e, "param", "None"),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
code=error_code,
headers=getattr(e, "headers", None),
)

View file

@ -190,3 +190,26 @@ def test_get_projected_spend_over_limit_includes_current_spend(monkeypatch):
projected_spend, projected_exceeded_date = result
assert projected_spend == 290.0
assert projected_exceeded_date == real_datetime.date(2026, 4, 21)
def test_handle_exception_on_proxy_preserves_429_status_code():
"""Exceptions with status_code=429 should not be mapped to 500."""
from litellm.proxy.utils import handle_exception_on_proxy
class FakeRateLimitError(Exception):
def __init__(self):
self.status_code = 429
self.message = "Rate limit exceeded"
self.param = None
super().__init__(self.message)
result = handle_exception_on_proxy(FakeRateLimitError())
assert result.code == "429"
def test_handle_exception_on_proxy_defaults_to_500_without_status_code():
"""Exceptions without status_code should map to 500."""
from litellm.proxy.utils import handle_exception_on_proxy
result = handle_exception_on_proxy(Exception("unknown error"))
assert result.code == "500"