diff --git a/litellm/proxy/auth/auth_exception_handler.py b/litellm/proxy/auth/auth_exception_handler.py index 30bf02b2b0f..ed743764fce 100644 --- a/litellm/proxy/auth/auth_exception_handler.py +++ b/litellm/proxy/auth/auth_exception_handler.py @@ -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( diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index 6ee17c1f9d5..312fbd6b0d3 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -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 diff --git a/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py b/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py index 1056df7fc47..6b59f6d40dc 100644 --- a/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py +++ b/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py @@ -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) diff --git a/tests/unit/proxy/auth/test_auth_exception_handler.py b/tests/unit/proxy/auth/test_auth_exception_handler.py index 5ff37bb3897..92b4c93118f 100644 --- a/tests/unit/proxy/auth/test_auth_exception_handler.py +++ b/tests/unit/proxy/auth/test_auth_exception_handler.py @@ -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 diff --git a/tests/unit/proxy/auth/test_handle_jwt.py b/tests/unit/proxy/auth/test_handle_jwt.py index 5d0f7363fa0..a0ea6ce0403 100644 --- a/tests/unit/proxy/auth/test_handle_jwt.py +++ b/tests/unit/proxy/auth/test_handle_jwt.py @@ -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)