This commit is contained in:
Mavik 2026-04-07 16:42:14 +00:00 • committed by GitHub
commit 61611be1af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View file

@ -5293,11 +5293,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"