fix(proxy): restore auth exception handling after staging merge

This commit is contained in:
Zhexun Hu 2026-09-22 01:41:37 +08:00
parent a42ef0bfee
commit 033fe540ea
2 changed files with 41 additions and 2 deletions

View file

@ -19,7 +19,11 @@ from litellm.proxy._types import (
ProxyException,
UserAPIKeyAuth,
)
from litellm.proxy.auth.auth_utils import _get_request_ip_address
from litellm.proxy.auth.auth_utils import (
_get_request_ip_address,
is_invalid_virtual_key_error,
mark_invalid_virtual_key_error,
)
from litellm.proxy.common_utils.http_parsing_utils import (
_safe_get_request_headers, # pyright: ignore[reportPrivateUsage] # canonical non-throwing request header reader
)
@ -40,6 +44,39 @@ else:
Span = Any
def _as_proxy_exception(e: Exception) -> ProxyException:
"""Convert an authentication failure into the ProxyException the client receives."""
if isinstance(e, litellm.BudgetExceededError):
return ProxyException(
message=e.message,
type=ProxyErrorTypes.budget_exceeded,
param=None,
code=getattr(e, "status_code", status.HTTP_429_TOO_MANY_REQUESTS),
)
if isinstance(e, HTTPException):
return ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
code=getattr(e, "status_code", status.HTTP_401_UNAUTHORIZED),
)
if isinstance(e, ProxyException):
return e
if PrismaDBExceptionHandler.is_database_service_unavailable_error(e):
return ProxyException(
message=PrismaDBExceptionHandler.database_unavailable_message(e),
type=ProxyErrorTypes.no_db_connection,
param="None",
code=status.HTTP_503_SERVICE_UNAVAILABLE,
)
return ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
code=status.HTTP_401_UNAUTHORIZED,
)
def _with_auth_failure_metadata(
request_data: dict[str, object],
requester_ip: str | None,

View file

@ -689,7 +689,7 @@ async def test_budget_auth_failure_logs_spend_metadata_from_request_header(
{"allow_requests_on_db_unavailable": False},
),
):
with pytest.raises(ProxyException):
with pytest.raises(ProxyException) as exc_info:
await UserAPIKeyAuthExceptionHandler._handle_authentication_error(
BudgetExceededError(message="Budget exceeded", current_cost=100, max_budget=100),
request,
@ -699,6 +699,8 @@ async def test_budget_auth_failure_logs_spend_metadata_from_request_header(
"sk-over-budget",
)
assert exc_info.value.code == str(status.HTTP_429_TOO_MANY_REQUESTS)
assert exc_info.value.type == ProxyErrorTypes.budget_exceeded
logged_request_data = mock_hook.call_args.kwargs["request_data"]
assert logged_request_data["metadata"]["spend_logs_metadata"] == expected_spend_logs_metadata
assert logged_request_data["metadata"]["requester_ip_address"] == "10.1.2.3"