mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): keep auth_error for 401 and 403 and answer database outages with 503 in route catch-alls
/key/list and /key/aliases now share the catch-all helper too, so they stop adding the Authentication Error prefix and typing a 400 as internal_server_error
This commit is contained in:
parent
76a4aa8d60
commit
3ac967b268
7 changed files with 110 additions and 44 deletions
|
|
@ -9,7 +9,8 @@ from typing import Final
|
|||
from fastapi import HTTPException, status
|
||||
|
||||
from litellm.constants import STRINGIFIED_NONE
|
||||
from litellm.proxy._types import ProxyException
|
||||
from litellm.proxy._types import ProxyErrorTypes, ProxyException
|
||||
from litellm.proxy.db.exception_handler import PrismaDBExceptionHandler
|
||||
|
||||
LITELLM_CALL_ID_HEADER: Final = "x-litellm-call-id"
|
||||
|
||||
|
|
@ -21,6 +22,8 @@ _OPENAI_ERROR_TYPE_BY_STATUS: Final[Mapping[int, str]] = MappingProxyType(
|
|||
}
|
||||
)
|
||||
|
||||
_AUTH_FAILURE_STATUS_CODES: Final = frozenset({status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN})
|
||||
|
||||
|
||||
def attribute_of(value: object, name: str, default: object = None) -> object:
|
||||
return getattr(value, name, default)
|
||||
|
|
@ -67,19 +70,37 @@ def proxy_exception_for(exc: Exception, default_status_code: int) -> ProxyExcept
|
|||
return exc
|
||||
case HTTPException(status_code=status_code, detail=detail):
|
||||
return _openai_shaped_proxy_exception(exc, str(detail), status_code)
|
||||
case _ if _is_database_outage(exc):
|
||||
return ProxyException(
|
||||
message=str(exc),
|
||||
type=ProxyErrorTypes.no_db_connection.value,
|
||||
param=None,
|
||||
code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
)
|
||||
case _:
|
||||
return _openai_shaped_proxy_exception(exc, str(exc), default_status_code)
|
||||
|
||||
|
||||
def _is_database_outage(exc: Exception) -> bool:
|
||||
raised_by_prisma: Final = type(exc).__module__.partition(".")[0] == "prisma"
|
||||
return raised_by_prisma and PrismaDBExceptionHandler.is_database_service_unavailable_error(exc)
|
||||
|
||||
|
||||
def _openai_shaped_proxy_exception(exc: Exception, message: str, status_code: int) -> ProxyException:
|
||||
return ProxyException(
|
||||
message=message,
|
||||
type=openai_error_type(exc, status_code),
|
||||
type=_catch_all_error_type(exc, status_code),
|
||||
param=openai_error_param(exc),
|
||||
code=status_code,
|
||||
)
|
||||
|
||||
|
||||
def _catch_all_error_type(exc: Exception, status_code: int) -> str:
|
||||
if error_status_code(exc, status_code) in _AUTH_FAILURE_STATUS_CODES:
|
||||
return ProxyErrorTypes.auth_error.value
|
||||
return openai_error_type(exc, status_code)
|
||||
|
||||
|
||||
def litellm_call_id_headers(litellm_call_id: str | None) -> dict[str, str] | None: # mutable-ok: ProxyException.headers
|
||||
if litellm_call_id is None:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -7211,21 +7211,7 @@ async def list_keys(
|
|||
|
||||
except Exception as e:
|
||||
verbose_proxy_logger.exception("Error in list_keys: %s", e)
|
||||
if isinstance(e, HTTPException):
|
||||
raise ProxyException(
|
||||
message=getattr(e, "detail", f"error({e})"),
|
||||
type=ProxyErrorTypes.internal_server_error,
|
||||
param=getattr(e, "param", "None"),
|
||||
code=getattr(e, "status_code", fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR),
|
||||
)
|
||||
elif isinstance(e, ProxyException):
|
||||
raise e
|
||||
raise ProxyException(
|
||||
message="Authentication Error, " + str(e),
|
||||
type=ProxyErrorTypes.internal_server_error,
|
||||
param=getattr(e, "param", "None"),
|
||||
code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
raise proxy_exception_for(e, default_status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR) from e
|
||||
|
||||
|
||||
async def _apply_non_admin_alias_scope(
|
||||
|
|
@ -7367,21 +7353,7 @@ async def key_aliases(
|
|||
|
||||
except Exception as e:
|
||||
verbose_proxy_logger.exception("Error in key_aliases: %s", e)
|
||||
if isinstance(e, HTTPException):
|
||||
raise ProxyException(
|
||||
message=getattr(e, "detail", f"error({e})"),
|
||||
type=ProxyErrorTypes.internal_server_error,
|
||||
param=getattr(e, "param", "None"),
|
||||
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
|
||||
)
|
||||
elif isinstance(e, ProxyException):
|
||||
raise e
|
||||
raise ProxyException(
|
||||
message="Authentication Error, " + str(e),
|
||||
type=ProxyErrorTypes.internal_server_error,
|
||||
param=getattr(e, "param", "None"),
|
||||
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
raise proxy_exception_for(e, default_status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) from e
|
||||
|
||||
|
||||
def _validate_sort_params(sort_by: str | None, sort_order: str) -> dict[str, str] | None:
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
import json
|
||||
|
||||
import httpx
|
||||
import prisma.errors
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
import litellm
|
||||
from litellm.proxy._types import ProxyErrorTypes, ProxyException
|
||||
from litellm.proxy.common_utils.openai_error_payload import (
|
||||
error_status_code,
|
||||
|
|
@ -211,7 +214,8 @@ def test_a_proxy_error_keeps_the_call_id_it_was_raised_with():
|
|||
[
|
||||
(400, "invalid_request_error"),
|
||||
(404, "invalid_request_error"),
|
||||
(403, "permission_error"),
|
||||
(401, "auth_error"),
|
||||
(403, "auth_error"),
|
||||
(422, "invalid_request_error"),
|
||||
(500, "internal_server_error"),
|
||||
],
|
||||
|
|
@ -272,3 +276,29 @@ def test_the_wrapped_error_serializes_as_an_openai_error_object(exc: Exception):
|
|||
|
||||
assert body["error"]["param"] is None
|
||||
assert body["error"]["type"] == "invalid_request_error"
|
||||
|
||||
|
||||
def test_a_provider_credential_failure_keeps_the_auth_label_at_the_status_its_route_answers_with():
|
||||
provider_failure = litellm.AuthenticationError(
|
||||
message="invalid x-api-key", llm_provider="anthropic", model="claude-haiku-5-5"
|
||||
)
|
||||
|
||||
wrapped = proxy_exception_for(provider_failure, default_status_code=400)
|
||||
|
||||
assert (wrapped.code, wrapped.type) == ("400", "auth_error")
|
||||
|
||||
|
||||
def test_a_database_outage_answers_503_so_callers_retry_instead_of_fixing_their_request():
|
||||
wrapped = proxy_exception_for(
|
||||
prisma.errors.PrismaError("Can't reach database server at `db:5432`"), default_status_code=400
|
||||
)
|
||||
|
||||
assert (wrapped.code, wrapped.type) == ("503", "no_db_connection")
|
||||
|
||||
|
||||
def test_a_network_failure_outside_the_database_is_not_reported_as_a_database_outage():
|
||||
callback_unreachable = httpx.ConnectError("[Errno 111] Connection refused")
|
||||
|
||||
wrapped = proxy_exception_for(callback_unreachable, default_status_code=500)
|
||||
|
||||
assert (wrapped.code, wrapped.type) == ("500", "internal_server_error")
|
||||
|
|
|
|||
|
|
@ -6540,7 +6540,7 @@ async def test_list_keys_with_invalid_status():
|
|||
)
|
||||
|
||||
# Verify ProxyException properties
|
||||
assert exc_info.value.code == "400"
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("400", "invalid_request_error")
|
||||
assert "Invalid status value" in str(exc_info.value.message)
|
||||
assert "deleted" in str(exc_info.value.message)
|
||||
|
||||
|
|
|
|||
|
|
@ -10157,3 +10157,23 @@ async def test_delete_model_reports_an_unknown_model_id_as_a_request_error_not_a
|
|||
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("400", "invalid_request_error")
|
||||
assert "Authentication Error" not in exc_info.value.message
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_model_reports_an_unknown_model_id_as_a_request_error_not_an_auth_error(monkeypatch):
|
||||
from litellm.proxy import proxy_server
|
||||
from litellm.proxy.management_endpoints.model_management_endpoints import update_model
|
||||
|
||||
prisma = MagicMock()
|
||||
prisma.db.litellm_proxymodeltable.find_unique = AsyncMock(return_value=None)
|
||||
monkeypatch.setattr(proxy_server, "prisma_client", prisma)
|
||||
monkeypatch.setattr(proxy_server, "llm_router", None)
|
||||
|
||||
with pytest.raises(ProxyException) as exc_info:
|
||||
await update_model(
|
||||
model_params=updateDeployment(model_info=ModelInfo(id="no-such-model")),
|
||||
user_api_key_dict=UserAPIKeyAuth(user_id="admin-1", user_role=LitellmUserRoles.PROXY_ADMIN),
|
||||
)
|
||||
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("400", "invalid_request_error")
|
||||
assert exc_info.value.message == "model not found"
|
||||
|
|
|
|||
|
|
@ -18651,3 +18651,26 @@ async def test_team_info_reports_a_missing_team_as_a_request_error_not_an_auth_e
|
|||
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("404", "invalid_request_error")
|
||||
assert "Authentication Error" not in exc_info.value.message
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_team_info_keeps_the_auth_label_for_a_caller_outside_the_team(monkeypatch):
|
||||
from fastapi import Request
|
||||
|
||||
from litellm.proxy._types import ProxyException
|
||||
from litellm.proxy.management_endpoints import team_endpoints
|
||||
|
||||
other_team = MagicMock()
|
||||
other_team.model_dump.return_value = {"team_id": "team-a", "members_with_roles": []}
|
||||
mock_prisma_client = MagicMock()
|
||||
mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=other_team)
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
|
||||
|
||||
with pytest.raises(ProxyException) as exc_info:
|
||||
await team_endpoints.team_info(
|
||||
http_request=MagicMock(spec=Request),
|
||||
team_id="team-a",
|
||||
user_api_key_dict=UserAPIKeyAuth(user_id="outsider", user_role=LitellmUserRoles.INTERNAL_USER),
|
||||
)
|
||||
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("403", "auth_error")
|
||||
|
|
|
|||
|
|
@ -3838,17 +3838,17 @@ async def test_update_config_reports_an_unsupported_router_setting_as_a_request_
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_config_reports_a_non_admin_caller_as_a_permission_error():
|
||||
from fastapi import Request
|
||||
async def test_get_config_callbacks_reports_an_unreadable_config_as_a_request_error_not_an_auth_error(monkeypatch):
|
||||
from litellm.proxy import proxy_server
|
||||
from litellm.proxy._types import ProxyException
|
||||
from litellm.proxy.proxy_server import get_config
|
||||
|
||||
from litellm.proxy._types import ConfigYAML, ProxyException
|
||||
from litellm.proxy.proxy_server import update_config
|
||||
unreadable_config = MagicMock()
|
||||
unreadable_config.get_config = AsyncMock(side_effect=ValueError("config row is not valid YAML"))
|
||||
monkeypatch.setattr(proxy_server, "proxy_config", unreadable_config)
|
||||
|
||||
with pytest.raises(ProxyException) as exc_info:
|
||||
await update_config(
|
||||
config_info=ConfigYAML(),
|
||||
request=MagicMock(spec=Request),
|
||||
user_api_key_dict=UserAPIKeyAuth(user_id="viewer-1", user_role=LitellmUserRoles.INTERNAL_USER),
|
||||
)
|
||||
await get_config(user_api_key_dict=UserAPIKeyAuth(user_id="admin-1", user_role=LitellmUserRoles.PROXY_ADMIN))
|
||||
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("403", "permission_error")
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("400", "invalid_request_error")
|
||||
assert exc_info.value.message == "config row is not valid YAML"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue