refactor(auth): keep the _as_proxy_exception name so main's tests still find it

This commit is contained in:
mateo-berri 2026-10-29 23:53:44 -07:00
parent 428b3c303a
commit 59f15043fe
5 changed files with 18 additions and 18 deletions

View file

@ -47,7 +47,7 @@ else:
Span = Any
def auth_failure_as_proxy_exception(e: Exception) -> ProxyException:
def _as_proxy_exception(e: Exception) -> ProxyException:
"""Convert an authentication failure into the ProxyException the client receives."""
if isinstance(e, litellm.BudgetExceededError):
return ProxyException(
@ -262,9 +262,7 @@ class UserAPIKeyAuthExceptionHandler:
if transformed_exception is not None:
e = transformed_exception
final_exception: Final = mark_invalid_virtual_key_error(
auth_failure_as_proxy_exception(e), is_invalid_virtual_key
)
final_exception: Final = mark_invalid_virtual_key_error(_as_proxy_exception(e), is_invalid_virtual_key)
# If a quiet-logged malformed-key transform yields non-401, escalate to ERROR
if is_quiet_log and str(final_exception.code) != str(status.HTTP_401_UNAUTHORIZED):
verbose_proxy_logger.error(

View file

@ -51,7 +51,9 @@ from litellm.proxy.auth.auth_checks import (
key_model_aliases_for_auth_check,
team_model_aliases_for_auth_check,
)
from litellm.proxy.auth.auth_exception_handler import auth_failure_as_proxy_exception
from litellm.proxy.auth.auth_exception_handler import (
_as_proxy_exception, # pyright: ignore[reportPrivateUsage] # one auth-failure mapping, shared with user_api_key_auth
)
from litellm.proxy.auth.auth_utils import (
_BANNED_REQUEST_BODY_PARAMS, # pyright: ignore[reportPrivateUsage] # one canonical list, shared with the request-body check
)
@ -1168,7 +1170,7 @@ async def _key_may_call_model(
except Exception as failure:
if isinstance(failure, HTTPException) and failure.status_code == status.HTTP_403_FORBIDDEN:
return False
raise auth_failure_as_proxy_exception(failure) from failure
raise _as_proxy_exception(failure) from failure
return True

View file

@ -1093,7 +1093,7 @@ def _user_read_raising(error: Exception) -> tuple[MagicMock, MagicMock]:
ids=["connect_error", "read_timeout", "p1001_as_data_error"],
)
async def test_get_user_object_surfaces_a_db_outage_as_503_not_as_a_missing_user(outage):
from litellm.proxy.auth.auth_exception_handler import auth_failure_as_proxy_exception
from litellm.proxy.auth.auth_exception_handler import _as_proxy_exception
prisma_client, cache = _user_read_raising(outage)
@ -1109,7 +1109,7 @@ async def test_get_user_object_surfaces_a_db_outage_as_503_not_as_a_missing_user
assert raised.value is outage
assert PrismaDBExceptionHandler.find_database_service_unavailable_error_in_chain(raised.value) is outage
surfaced = auth_failure_as_proxy_exception(raised.value)
surfaced = _as_proxy_exception(raised.value)
assert (surfaced.code, surfaced.type) == ("503", ProxyErrorTypes.no_db_connection)
@ -1123,7 +1123,7 @@ async def test_get_user_object_surfaces_a_db_outage_as_503_not_as_a_missing_user
ids=["query_level_data_error", "runtime_error"],
)
async def test_get_user_object_still_reports_a_non_outage_read_failure_as_a_missing_user(failure):
from litellm.proxy.auth.auth_exception_handler import auth_failure_as_proxy_exception
from litellm.proxy.auth.auth_exception_handler import _as_proxy_exception
prisma_client, cache = _user_read_raising(failure)
@ -1138,7 +1138,7 @@ async def test_get_user_object_still_reports_a_non_outage_read_failure_as_a_miss
)
assert raised.value.__context__ is failure
surfaced = auth_failure_as_proxy_exception(raised.value)
surfaced = _as_proxy_exception(raised.value)
assert (surfaced.code, surfaced.type) == ("401", ProxyErrorTypes.auth_error)
@ -9354,14 +9354,14 @@ def _restricted_member_check_deps() -> dict[str, object]:
@pytest.mark.asyncio
async def test_check_team_member_model_access_fails_closed_when_the_membership_read_hits_a_db_outage():
from litellm.proxy.auth.auth_checks import check_team_member_model_access
from litellm.proxy.auth.auth_exception_handler import auth_failure_as_proxy_exception
from litellm.proxy.auth.auth_exception_handler import _as_proxy_exception
with pytest.raises(httpx.ConnectError) as raised:
await check_team_member_model_access(
model="claude-sonnet-5", llm_router=None, **_restricted_member_check_deps()
)
surfaced = auth_failure_as_proxy_exception(raised.value)
surfaced = _as_proxy_exception(raised.value)
assert (surfaced.code, surfaced.type) == ("503", ProxyErrorTypes.no_db_connection)

View file

@ -38,7 +38,7 @@ from litellm.proxy._types import (
ProxyException,
UserAPIKeyAuth,
)
from litellm.proxy.auth.auth_exception_handler import UserAPIKeyAuthExceptionHandler, auth_failure_as_proxy_exception
from litellm.proxy.auth.auth_exception_handler import UserAPIKeyAuthExceptionHandler, _as_proxy_exception
from litellm.proxy.auth.model_access_denied import ModelAccessDeniedHTTPException
@ -849,7 +849,7 @@ async def test_auth_error_preserves_classification_with_non_object_context(
"database": httpx.ConnectError("Database unavailable"),
}
error: Final = errors[error_kind]
expected: Final = auth_failure_as_proxy_exception(error)
expected: Final = _as_proxy_exception(error)
recorder: Final = _AuthFailureRecorder()
monkeypatch.setattr(litellm, "callbacks", [recorder])
monkeypatch.setattr(proxy_server, "general_settings", {"allow_requests_on_db_unavailable": False})
@ -1292,9 +1292,9 @@ def test_as_proxy_exception_keeps_jwt_scope_denial_message_shape():
status_code=status.HTTP_403_FORBIDDEN,
detail=detail,
)
plain = auth_failure_as_proxy_exception(HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=detail))
plain = _as_proxy_exception(HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=detail))
converted = auth_failure_as_proxy_exception(denial)
converted = _as_proxy_exception(denial)
assert converted.to_dict() == plain.to_dict()
assert converted.internal_message == denial.internal_message

View file

@ -3447,7 +3447,7 @@ async def test_auth_builder_single_team_fallback_membership_outage_raises_instea
get_team_object succeeds but the membership read hits a database outage: the
outage propagates (auth maps it to 503) instead of the team being dropped.
"""
from litellm.proxy.auth.auth_exception_handler import auth_failure_as_proxy_exception
from litellm.proxy.auth.auth_exception_handler import _as_proxy_exception
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
from litellm.proxy.utils import ProxyLogging
@ -3529,7 +3529,7 @@ async def test_auth_builder_single_team_fallback_membership_outage_raises_instea
)
mock_get_team.assert_called()
surfaced = auth_failure_as_proxy_exception(raised.value)
surfaced = _as_proxy_exception(raised.value)
assert (surfaced.code, surfaced.type) == ("503", ProxyErrorTypes.no_db_connection)