From 6a9cb48cf3d1a2438db3de4199343cae89828b8c Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 3 Sep 2026 04:31:30 -0700 Subject: [PATCH 1/7] fix(proxy): name shared-handler errors by the status they answer handle_exception_on_proxy pinned type=internal_server_error on every exception it wrapped, so a management route rejecting a caller's own argument answered {"type":"internal_server_error","code":"400"} and an SDK branching on the type read its own bad request as a gateway outage. Derive the type from the status the response actually carries, keeping a type the exception already names. --- litellm/proxy/utils.py | 15 +++-- .../credential_endpoints/test_endpoints.py | 28 ++++++++ .../proxy/utils/helpers/test_error_helpers.py | 64 ++++++++++++++++++- 3 files changed, 100 insertions(+), 7 deletions(-) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 5c0fbd64744..ff5ce75a8c8 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -37,7 +37,11 @@ from litellm.proxy._types import ( SpendLogsMetadata, SpendLogsPayload, ) -from litellm.proxy.common_utils.openai_error_payload import openai_error_param +from litellm.proxy.common_utils.openai_error_payload import ( + error_status_code, + openai_error_param, + openai_error_type, +) from litellm.proxy.spend_tracking.spend_log_error_logger import spend_log_error from litellm.types.guardrails import GuardrailEventHooks from litellm.types.proxy.model_listing import ModelInfoResponse @@ -7096,18 +7100,19 @@ def handle_exception_on_proxy(e: Exception) -> ProxyException: verbose_proxy_logger.exception("Exception: %s", e) if isinstance(e, HTTPException): + http_status_code: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR) return ProxyException( message=getattr(e, "detail", f"error({e})"), - type=ProxyErrorTypes.internal_server_error, + type=openai_error_type(e, http_status_code), param=openai_error_param(e), - code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR), + code=http_status_code, ) elif isinstance(e, ProxyException): return e - _status_code: Final = getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR) + _status_code: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR) return ProxyException( message=str(e), - type=ProxyErrorTypes.internal_server_error, + type=openai_error_type(e, _status_code), param=openai_error_param(e), code=_status_code, ) diff --git a/tests/test_litellm/proxy/credential_endpoints/test_endpoints.py b/tests/test_litellm/proxy/credential_endpoints/test_endpoints.py index dcd8e6881bd..fd38c22398a 100644 --- a/tests/test_litellm/proxy/credential_endpoints/test_endpoints.py +++ b/tests/test_litellm/proxy/credential_endpoints/test_endpoints.py @@ -86,3 +86,31 @@ def test_update_credential_still_answers_200_on_a_successful_write(): assert response.status_code == 200, response.text assert response.json()["success"] is True + + +def _get_credential_by_name(name: str): + missing = object() + previous_override = app.dependency_overrides.get(user_api_key_auth, missing) + app.dependency_overrides[user_api_key_auth] = _as_admin + try: + return client.get(f"/credentials/by_name/{name}", headers={"Authorization": "Bearer test-key"}) + finally: + if previous_override is missing: + app.dependency_overrides.pop(user_api_key_auth, None) + else: + app.dependency_overrides[user_api_key_auth] = previous_override + + +def test_looking_up_a_missing_credential_is_labelled_a_client_error_not_a_gateway_failure(): + """Naming a credential that does not exist is the caller's own mistake, and the shared + handler used to label every one of these internal_server_error, so an SDK branching on + the type read a 404 it can never retry past as a gateway outage and backed off forever.""" + response = _get_credential_by_name("definitely-not-there") + + assert response.status_code == 404, f"lookup answered {response.status_code}: {response.text}" + assert response.json()["error"] == { + "message": "Credential not found. Got credential name: definitely-not-there", + "type": "invalid_request_error", + "param": None, + "code": "404", + } diff --git a/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py b/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py index dc30798df55..3333bcc80e5 100644 --- a/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py +++ b/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py @@ -3,6 +3,7 @@ import json import pytest from fastapi import HTTPException +import litellm from litellm.proxy._types import ProxyErrorTypes, ProxyException from litellm.proxy.utils import get_error_message_str, handle_exception_on_proxy @@ -93,7 +94,7 @@ def test_handle_exception_on_proxy_happy_path_http_exception(): assert snapshot == { "is_proxy_exception": True, "message": "forbidden", - "type": ProxyErrorTypes.internal_server_error.value, + "type": "permission_error", "code": "403", } @@ -153,7 +154,7 @@ def test_handle_exception_on_proxy_uses_attached_status_code_when_present(): assert snapshot == { "code": "418", "message": "teapot", - "type": ProxyErrorTypes.internal_server_error.value, + "type": "invalid_request_error", } @@ -171,3 +172,62 @@ def test_handle_exception_on_proxy_error_path_none_input_wraps_as_500(): "code": "500", "type": ProxyErrorTypes.internal_server_error.value, } + + +@pytest.mark.parametrize( + "status_code, expected_type", + [ + (400, "invalid_request_error"), + (401, "authentication_error"), + (403, "permission_error"), + (404, "invalid_request_error"), + (409, "invalid_request_error"), + (422, "invalid_request_error"), + (429, "rate_limit_error"), + (500, ProxyErrorTypes.internal_server_error.value), + (503, ProxyErrorTypes.internal_server_error.value), + ], +) +def test_handle_exception_on_proxy_labels_an_http_exception_by_the_status_it_answers( + status_code: int, expected_type: str +): + """A management route that rejects a caller's own argument answers 400, and calling that + internal_server_error tells the caller's retry loop the gateway is down when nothing but + their request is wrong.""" + result = handle_exception_on_proxy(HTTPException(status_code=status_code, detail="boom")) + + assert (result.code, result.type) == (str(status_code), expected_type) + + +def _exception_carrying_status(carried: int) -> Exception: + class _CarriedStatus(Exception): + status_code = carried + + return _CarriedStatus("boom") + + +@pytest.mark.parametrize( + "status_code, expected_type", + [ + (400, "invalid_request_error"), + (404, "invalid_request_error"), + (429, "rate_limit_error"), + (500, ProxyErrorTypes.internal_server_error.value), + ], +) +def test_handle_exception_on_proxy_labels_a_carried_status_the_same_way(status_code: int, expected_type: str): + """Routes also reach this handler with plain exceptions carrying their own status, and + those must be named by that status rather than all landing on the 500 label.""" + result = handle_exception_on_proxy(_exception_carrying_status(status_code)) + + assert (result.code, result.type) == (str(status_code), expected_type) + + +def test_handle_exception_on_proxy_keeps_the_type_the_exception_already_names(): + """A rate limit litellm raised already names itself, and overwriting that with the status + map's guess would drop what the client branches on.""" + result = handle_exception_on_proxy( + litellm.RateLimitError(message="slow down", llm_provider="openai", model="gpt-4o") + ) + + assert (result.code, result.type) == ("429", "throttling_error") From 07df4ff09c0481b788c4e64dd0e5f1bbe34ce58f Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 22:04:05 -0700 Subject: [PATCH 2/7] test(proxy): pin a 500 for exceptions whose status is None --- tests/unit/proxy/utils/helpers/test_error_helpers.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/unit/proxy/utils/helpers/test_error_helpers.py b/tests/unit/proxy/utils/helpers/test_error_helpers.py index 5d530e6d89f..9d7fb9cbadc 100644 --- a/tests/unit/proxy/utils/helpers/test_error_helpers.py +++ b/tests/unit/proxy/utils/helpers/test_error_helpers.py @@ -202,7 +202,7 @@ def test_handle_exception_on_proxy_labels_an_http_exception_by_the_status_it_ans assert (result.code, result.type) == (str(status_code), expected_type) -def _exception_carrying_status(carried: int) -> Exception: +def _exception_carrying_status(carried: int | None) -> Exception: class _CarriedStatus(Exception): status_code = carried @@ -236,6 +236,15 @@ def test_handle_exception_on_proxy_keeps_the_type_the_exception_already_names(): assert (result.code, result.type) == ("429", "throttling_error") +def test_handle_exception_on_proxy_answers_500_for_an_exception_whose_status_is_none(): + """A guardrail whose upstream refuses the connection raises with status_code=None, and + passing that through as the code crashed the proxy's error handler, which then blamed the + auth database with a 503 instead of naming the unreachable guardrail.""" + result = handle_exception_on_proxy(_exception_carrying_status(None)) + + assert (result.code, result.type, result.message) == ("500", ProxyErrorTypes.internal_server_error.value, "boom") + + @pytest.mark.parametrize( "exc", [ From 1d416418e484999fa8933e9f60fd0473e27b132f Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 21 Oct 2026 01:30:26 -0700 Subject: [PATCH 3/7] fix(proxy): keep internal_server_error for timeouts and 5xx, redact hosts in shared handler errors Provider 5xx and timeouts keep internal_server_error so retry loops still retry them, the shared handler redacts internal hosts from raw exception messages, and /key/list routes its HTTPException through the shared handler instead of its own copy of the old label. --- .../common_utils/openai_error_payload.py | 6 +++ .../key_management_endpoints.py | 7 +-- litellm/proxy/utils.py | 10 ++-- .../credential_endpoints/test_endpoints.py | 6 ++- .../test_key_management_endpoints.py | 4 +- .../proxy/utils/helpers/test_error_helpers.py | 49 +++++++++++++++++-- 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/litellm/proxy/common_utils/openai_error_payload.py b/litellm/proxy/common_utils/openai_error_payload.py index 2f01dbe11f7..3f852ca7fec 100644 --- a/litellm/proxy/common_utils/openai_error_payload.py +++ b/litellm/proxy/common_utils/openai_error_payload.py @@ -45,6 +45,12 @@ def openai_error_type(exc: object, status_code: int) -> str: return openai_error_type_for_status(status_code) +def caller_error_type(exc: object, status_code: int) -> str: + if status_code == status.HTTP_408_REQUEST_TIMEOUT or status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR: + return "internal_server_error" + return openai_error_type(exc, status_code) + + def openai_error_type_for_status(status_code: int) -> str: mapped: Final = _OPENAI_ERROR_TYPE_BY_STATUS.get(status_code) if mapped is not None: diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index 824b310a1dd..1f3a882469f 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -7225,12 +7225,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), - ) + raise handle_exception_on_proxy(e) elif isinstance(e, ProxyException): raise e raise ProxyException( diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index a10f9e9ee9d..00b1de408c5 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -84,10 +84,10 @@ from litellm.proxy._types import ( from litellm.proxy.bug_report_config import build_proxy_bug_report from litellm.proxy.common_utils.openai_error_payload import ( attribute_of, + caller_error_type, error_status_code, litellm_call_id_headers, openai_error_param, - openai_error_type, with_litellm_call_id, ) from litellm.proxy.spend_tracking.spend_log_error_logger import spend_log_error @@ -133,7 +133,7 @@ from litellm import ( Router, ) from litellm._internal_context import service_target -from litellm._logging import redact_string, verbose_proxy_logger +from litellm._logging import redact_internal_details_from_client_message, redact_string, verbose_proxy_logger from litellm._service_logger import ServiceLogging, ServiceTypes from litellm.caching.caching import DualCache, RedisCache from litellm.caching.dual_cache import LimitedSizeOrderedDict @@ -9017,7 +9017,7 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) http_status_code: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR) return ProxyException( message=getattr(e, "detail", f"error({e})"), - type=openai_error_type(e, http_status_code), + type=caller_error_type(e, http_status_code), param=openai_error_param(e), headers=headers, code=http_status_code, @@ -9028,8 +9028,8 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) if should_report_bug(e): verbose_proxy_logger.error(bug_report_notice(build_proxy_bug_report(e))) return ProxyException( - message=strip_bug_report_notice(str(e)), - type=openai_error_type(e, _status_code), + message=redact_internal_details_from_client_message(strip_bug_report_notice(str(e))), + type=caller_error_type(e, _status_code), param=openai_error_param(e), headers=headers, code=_status_code, diff --git a/tests/unit/proxy/credential_endpoints/test_endpoints.py b/tests/unit/proxy/credential_endpoints/test_endpoints.py index d1993909ece..3c5343eb7e7 100644 --- a/tests/unit/proxy/credential_endpoints/test_endpoints.py +++ b/tests/unit/proxy/credential_endpoints/test_endpoints.py @@ -3016,11 +3016,13 @@ class TestCredentialDisplayName: assert listed["openai-prod"]["credential_values"]["api_key"] != "sk-old" -def test_looking_up_a_missing_credential_is_labelled_a_client_error_not_a_gateway_failure(restore_credential_list): +def test_looking_up_a_missing_credential_is_labelled_a_client_error_not_a_gateway_failure( + restore_credential_list: None, +): """Naming a credential that does not exist is the caller's own mistake, and the shared handler used to label every one of these internal_server_error, so an SDK branching on the type read a 404 it can never retry past as a gateway outage and backed off forever.""" - response = _call_as("GET", "/credentials/by_name/definitely-not-there") + response: Final = _call_as("GET", "/credentials/by_name/definitely-not-there") assert response.status_code == 404, f"lookup answered {response.status_code}: {response.text}" assert response.json()["error"] == { diff --git a/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py index d6ee9b1d29e..cafb96735c5 100644 --- a/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py @@ -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) @@ -18733,7 +18733,7 @@ async def test_list_keys_rejects_invalid_expires(): expires="expred", ) - assert exc_info.value.code == "400" + assert (exc_info.value.code, exc_info.value.type) == ("400", "invalid_request_error") assert "Invalid expires value" in str(exc_info.value.message) diff --git a/tests/unit/proxy/utils/helpers/test_error_helpers.py b/tests/unit/proxy/utils/helpers/test_error_helpers.py index 9d7fb9cbadc..a6667f7a46d 100644 --- a/tests/unit/proxy/utils/helpers/test_error_helpers.py +++ b/tests/unit/proxy/utils/helpers/test_error_helpers.py @@ -1,7 +1,10 @@ import asyncio import json +from typing import Final from unittest.mock import AsyncMock, MagicMock +import httpx +import openai import pytest from fastapi import HTTPException from prisma.errors import DataError @@ -184,6 +187,7 @@ def test_handle_exception_on_proxy_error_path_none_input_wraps_as_500(): (401, "authentication_error"), (403, "permission_error"), (404, "invalid_request_error"), + (408, ProxyErrorTypes.internal_server_error.value), (409, "invalid_request_error"), (422, "invalid_request_error"), (429, "rate_limit_error"), @@ -197,7 +201,7 @@ def test_handle_exception_on_proxy_labels_an_http_exception_by_the_status_it_ans """A management route that rejects a caller's own argument answers 400, and calling that internal_server_error tells the caller's retry loop the gateway is down when nothing but their request is wrong.""" - result = handle_exception_on_proxy(HTTPException(status_code=status_code, detail="boom")) + result: Final = handle_exception_on_proxy(HTTPException(status_code=status_code, detail="boom")) assert (result.code, result.type) == (str(status_code), expected_type) @@ -214,6 +218,7 @@ def _exception_carrying_status(carried: int | None) -> Exception: [ (400, "invalid_request_error"), (404, "invalid_request_error"), + (408, ProxyErrorTypes.internal_server_error.value), (429, "rate_limit_error"), (500, ProxyErrorTypes.internal_server_error.value), ], @@ -221,7 +226,7 @@ def _exception_carrying_status(carried: int | None) -> Exception: def test_handle_exception_on_proxy_labels_a_carried_status_the_same_way(status_code: int, expected_type: str): """Routes also reach this handler with plain exceptions carrying their own status, and those must be named by that status rather than all landing on the 500 label.""" - result = handle_exception_on_proxy(_exception_carrying_status(status_code)) + result: Final = handle_exception_on_proxy(_exception_carrying_status(status_code)) assert (result.code, result.type) == (str(status_code), expected_type) @@ -229,18 +234,54 @@ def test_handle_exception_on_proxy_labels_a_carried_status_the_same_way(status_c def test_handle_exception_on_proxy_keeps_the_type_the_exception_already_names(): """A rate limit litellm raised already names itself, and overwriting that with the status map's guess would drop what the client branches on.""" - result = handle_exception_on_proxy( + result: Final = handle_exception_on_proxy( litellm.RateLimitError(message="slow down", llm_provider="openai", model="gpt-4o") ) assert (result.code, result.type) == ("429", "throttling_error") +@pytest.mark.parametrize( + "exc, expected_code", + [ + ( + openai.InternalServerError( + message="upstream down", + response=httpx.Response(500, request=httpx.Request("GET", "https://api.openai.com/v1/batches/b")), + body={"type": "server_error"}, + ), + "500", + ), + (litellm.Timeout(message="took too long", model="gpt-6.1-sol", llm_provider="openai"), "408"), + ], + ids=["provider_5xx_naming_its_own_type", "provider_timeout"], +) +def test_handle_exception_on_proxy_keeps_internal_server_error_for_failures_the_caller_cannot_fix( + exc: Exception, expected_code: str +): + """Batch and fine-tuning routes pass provider errors straight through, and a retry loop + that treats internal_server_error as transient must keep retrying a provider outage or a + timeout instead of reading the provider's own type or a fix-your-request label.""" + result: Final = handle_exception_on_proxy(exc) + + assert (result.code, result.type) == (expected_code, ProxyErrorTypes.internal_server_error.value) + + +def test_handle_exception_on_proxy_redacts_the_internal_host_an_unreachable_upstream_names(): + """A guardrail or provider the gateway cannot reach names its private address in the + error, and any virtual key can call those routes, so the host must not reach the caller.""" + result: Final = handle_exception_on_proxy(ConnectionError("Cannot connect to host 10.12.4.7:8443")) + + assert result.code == "500" + assert "Cannot connect to host" in result.message + assert "10.12.4.7" not in result.message + + def test_handle_exception_on_proxy_answers_500_for_an_exception_whose_status_is_none(): """A guardrail whose upstream refuses the connection raises with status_code=None, and passing that through as the code crashed the proxy's error handler, which then blamed the auth database with a 503 instead of naming the unreachable guardrail.""" - result = handle_exception_on_proxy(_exception_carrying_status(None)) + result: Final = handle_exception_on_proxy(_exception_carrying_status(None)) assert (result.code, result.type, result.message) == ("500", ProxyErrorTypes.internal_server_error.value, "boom") From c997ed0eb117e49c5b49980233c01184cd2624b5 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 19:55:27 -0700 Subject: [PATCH 4/7] fix(proxy): label shared-handler errors like the chat route, pick the status before the type, redact only gateway failures --- litellm/proxy/_types.py | 20 +++-- .../common_utils/openai_error_payload.py | 14 ++-- .../key_management_endpoints.py | 4 +- litellm/proxy/utils.py | 25 +++--- .../proxy/utils/helpers/test_error_helpers.py | 84 ++++++++++++++----- 5 files changed, 102 insertions(+), 45 deletions(-) diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index 5ce6cd93a22..e49487afc72 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -3816,6 +3816,16 @@ class ManagementEndpointLoggingPayload(LiteLLMPydanticObjectBase): end_time: datetime | None = None +def router_error_status_code(message: str) -> int | None: + """The status a router failure answers with when its message names it: no deployment left maps to 429 + (https://github.com/BerriAI/litellm/issues/2487) and no deployment matching the request's tags to 401.""" + if "No healthy deployment available" in message or "No deployments available" in message: + return 429 + if RouterErrors.no_deployments_with_tag_routing.value in message: + return 401 + return None + + class ProxyException(Exception): # NOTE: DO NOT MODIFY THIS # This is used to map exactly to OPENAI Exceptions @@ -3837,20 +3847,14 @@ class ProxyException(Exception): # If we look on official python OpenAI lib, the code should be a string: # https://github.com/openai/openai-python/blob/195c05a64d39c87b2dfdf1eca2d339597f1fce03/src/openai/types/shared/error_object.py#L11 # Related LiteLLM issue: https://github.com/BerriAI/litellm/discussions/4834 - self.code = str(code) + router_status_code: Final = router_error_status_code(self.message) + self.code = str(code if router_status_code is None else router_status_code) if headers is not None: for k, v in headers.items(): if not isinstance(v, str): headers[k] = str(v) self.headers = headers or {} self.provider_specific_fields = provider_specific_fields - # rules for proxyExceptions - # Litellm router.py returns "No healthy deployment available" when there are no deployments available - # Should map to 429 errors https://github.com/BerriAI/litellm/issues/2487 - if "No healthy deployment available" in self.message or "No deployments available" in self.message: - self.code = "429" - elif RouterErrors.no_deployments_with_tag_routing.value in self.message: - self.code = "401" def to_dict(self) -> dict: """Converts the ProxyException instance to a dictionary.""" diff --git a/litellm/proxy/common_utils/openai_error_payload.py b/litellm/proxy/common_utils/openai_error_payload.py index 3f852ca7fec..aea60d5217f 100644 --- a/litellm/proxy/common_utils/openai_error_payload.py +++ b/litellm/proxy/common_utils/openai_error_payload.py @@ -9,7 +9,7 @@ from typing import Final from fastapi import status from litellm.constants import STRINGIFIED_NONE -from litellm.proxy._types import ProxyException +from litellm.proxy._types import ProxyException, router_error_status_code LITELLM_CALL_ID_HEADER: Final = "x-litellm-call-id" @@ -45,10 +45,14 @@ def openai_error_type(exc: object, status_code: int) -> str: return openai_error_type_for_status(status_code) -def caller_error_type(exc: object, status_code: int) -> str: - if status_code == status.HTTP_408_REQUEST_TIMEOUT or status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR: - return "internal_server_error" - return openai_error_type(exc, status_code) +def proxy_error_status_code(exc: object, message: str) -> int: + """The status ``ProxyException`` answers ``exc`` with once its router-message rules apply.""" + routed: Final = router_error_status_code(message) + return routed if routed is not None else error_status_code(exc, status.HTTP_500_INTERNAL_SERVER_ERROR) + + +def is_gateway_failure(status_code: int) -> bool: + return status_code == status.HTTP_408_REQUEST_TIMEOUT or status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR def openai_error_type_for_status(status_code: int) -> str: diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index 0736924bb3e..119e0da944d 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -7243,10 +7243,10 @@ async def list_keys( return response except Exception as e: - verbose_proxy_logger.exception("Error in list_keys: %s", e) if isinstance(e, HTTPException): raise handle_exception_on_proxy(e) - elif isinstance(e, ProxyException): + verbose_proxy_logger.exception("Error in list_keys: %s", e) + if isinstance(e, ProxyException): raise e raise ProxyException( message="Authentication Error, " + str(e), diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index c1c78938591..1487774cd64 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -85,10 +85,11 @@ from litellm.proxy._types import ( from litellm.proxy.bug_report_config import build_proxy_bug_report from litellm.proxy.common_utils.openai_error_payload import ( attribute_of, - caller_error_type, - error_status_code, + is_gateway_failure, litellm_call_id_headers, openai_error_param, + openai_error_type, + proxy_error_status_code, with_litellm_call_id, ) from litellm.proxy.spend_tracking.spend_log_error_logger import spend_log_error @@ -9179,12 +9180,14 @@ def _recreate_writer_on_read_only_transaction(prisma_client: "PrismaClient | Non asyncio.create_task(prisma_client.recreate_read_only_writer(reason="postgres_read_only_transaction")) +def _client_error_message(message: str, status_code: int) -> str: + return redact_internal_details_from_client_message(message) if is_gateway_failure(status_code) else message + + def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) -> ProxyException: """ Returns an Exception as ProxyException, this ensures all exceptions are OpenAI API compatible """ - from fastapi import status - verbose_proxy_logger.exception("Exception: %s", e) if PrismaDBExceptionHandler.is_read_only_transaction_error(e): from litellm.proxy.proxy_server import prisma_client @@ -9193,22 +9196,24 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) headers: Final = litellm_call_id_headers(litellm_call_id) if isinstance(e, HTTPException): - http_status_code: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR) + detail: Final = str(attribute_of(e, "detail", f"error({e})")) + http_status_code: Final = proxy_error_status_code(e, detail) return ProxyException( - message=getattr(e, "detail", f"error({e})"), - type=caller_error_type(e, http_status_code), + message=_client_error_message(detail, http_status_code), + type=openai_error_type(e, http_status_code), param=openai_error_param(e), headers=headers, code=http_status_code, ) elif isinstance(e, ProxyException): return with_litellm_call_id(e, litellm_call_id) - _status_code: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR) + message: Final = strip_bug_report_notice(str(e)) + _status_code: Final = proxy_error_status_code(e, message) if should_report_bug(e): verbose_proxy_logger.error(bug_report_notice(build_proxy_bug_report(e))) return ProxyException( - message=redact_internal_details_from_client_message(strip_bug_report_notice(str(e))), - type=caller_error_type(e, _status_code), + message=_client_error_message(message, _status_code), + type=openai_error_type(e, _status_code), param=openai_error_param(e), headers=headers, code=_status_code, diff --git a/tests/unit/proxy/utils/helpers/test_error_helpers.py b/tests/unit/proxy/utils/helpers/test_error_helpers.py index a6667f7a46d..6f6e2dcc469 100644 --- a/tests/unit/proxy/utils/helpers/test_error_helpers.py +++ b/tests/unit/proxy/utils/helpers/test_error_helpers.py @@ -11,7 +11,9 @@ from prisma.errors import DataError import litellm from litellm.proxy._types import ProxyErrorTypes, ProxyException +from litellm.proxy.common_request_processing import proxy_exception_from_route_error from litellm.proxy.utils import get_error_message_str, handle_exception_on_proxy +from litellm.types.router import RouterErrors def normalize(value): @@ -187,7 +189,7 @@ def test_handle_exception_on_proxy_error_path_none_input_wraps_as_500(): (401, "authentication_error"), (403, "permission_error"), (404, "invalid_request_error"), - (408, ProxyErrorTypes.internal_server_error.value), + (408, "invalid_request_error"), (409, "invalid_request_error"), (422, "invalid_request_error"), (429, "rate_limit_error"), @@ -218,7 +220,7 @@ def _exception_carrying_status(carried: int | None) -> Exception: [ (400, "invalid_request_error"), (404, "invalid_request_error"), - (408, ProxyErrorTypes.internal_server_error.value), + (408, "invalid_request_error"), (429, "rate_limit_error"), (500, ProxyErrorTypes.internal_server_error.value), ], @@ -242,29 +244,61 @@ def test_handle_exception_on_proxy_keeps_the_type_the_exception_already_names(): @pytest.mark.parametrize( - "exc, expected_code", + "exc", [ - ( - openai.InternalServerError( - message="upstream down", - response=httpx.Response(500, request=httpx.Request("GET", "https://api.openai.com/v1/batches/b")), - body={"type": "server_error"}, - ), - "500", + openai.InternalServerError( + message="upstream down", + response=httpx.Response(500, request=httpx.Request("GET", "https://api.openai.com/v1/batches/b")), + body={"type": "server_error"}, ), - (litellm.Timeout(message="took too long", model="gpt-6.1-sol", llm_provider="openai"), "408"), + litellm.Timeout(message="took too long", model="gpt-6.1-sol", llm_provider="openai"), + litellm.RateLimitError(message="slow down", llm_provider="openai", model="gpt-6.1-sol"), + litellm.AuthenticationError(message="bad key", llm_provider="openai", model="gpt-6.1-sol"), + litellm.BadRequestError(message="bad input", model="gpt-6.1-sol", llm_provider="openai"), + HTTPException(status_code=404, detail="no such batch"), ], - ids=["provider_5xx_naming_its_own_type", "provider_timeout"], + ids=["provider_5xx_naming_its_own_type", "timeout", "rate_limit", "bad_key", "bad_request", "http_404"], ) -def test_handle_exception_on_proxy_keeps_internal_server_error_for_failures_the_caller_cannot_fix( - exc: Exception, expected_code: str -): - """Batch and fine-tuning routes pass provider errors straight through, and a retry loop - that treats internal_server_error as transient must keep retrying a provider outage or a - timeout instead of reading the provider's own type or a fix-your-request label.""" - result: Final = handle_exception_on_proxy(exc) +def test_handle_exception_on_proxy_labels_a_failure_the_way_the_chat_route_does(exc: Exception): + """Batch, file, and management routes answer through this handler while chat answers through the route + error path, and one client calling both must read the same type for the same failure.""" + shared: Final = handle_exception_on_proxy(exc) + chat: Final = proxy_exception_from_route_error(exc) - assert (result.code, result.type) == (expected_code, ProxyErrorTypes.internal_server_error.value) + assert (shared.code, shared.type) == (chat.code, chat.type) + + +@pytest.mark.parametrize( + "message, expected", + [ + (f"{RouterErrors.no_deployments_with_provider_budget_routing.value}, try later", ("429", "rate_limit_error")), + (f"{RouterErrors.no_deployments_with_tag_routing.value}=['paid']", ("401", "authentication_error")), + ], + ids=["no_deployment_left", "no_deployment_for_tags"], +) +def test_handle_exception_on_proxy_names_a_router_failure_by_the_status_it_answers( + message: str, expected: tuple[str, str] +): + """A router failure that carries no status answers the 429 or 401 its message maps to, and a 429 labelled + internal_server_error reads as an outage to a client that branches on the type.""" + result: Final = handle_exception_on_proxy(ValueError(message)) + + assert (result.code, result.type) == expected + + +def test_handle_exception_on_proxy_keeps_the_path_a_rejected_request_names(): + """A provider rejecting a batch input names the caller's own bucket path, and redacting the /tmp part of it + leaves the caller nothing to fix their request with.""" + result: Final = handle_exception_on_proxy( + litellm.BadRequestError( + message="Invalid input_file_id gs://my-bucket/tmp/batch_input.jsonl", + model="gemini-3.1-pro-preview", + llm_provider="vertex_ai", + ) + ) + + assert result.code == "400" + assert "gs://my-bucket/tmp/batch_input.jsonl" in result.message def test_handle_exception_on_proxy_redacts_the_internal_host_an_unreachable_upstream_names(): @@ -277,6 +311,16 @@ def test_handle_exception_on_proxy_redacts_the_internal_host_an_unreachable_upst assert "10.12.4.7" not in result.message +def test_handle_exception_on_proxy_redacts_the_internal_host_a_route_puts_in_a_gateway_failure_detail(): + """Routes wrap an upstream failure as an HTTPException whose detail is the raw error, so a 5xx detail carries + the same private address an unwrapped connection error does.""" + result: Final = handle_exception_on_proxy(HTTPException(status_code=503, detail="Cannot connect to host 10.0.0.9")) + + assert result.code == "503" + assert "Cannot connect to host" in result.message + assert "10.0.0.9" not in result.message + + def test_handle_exception_on_proxy_answers_500_for_an_exception_whose_status_is_none(): """A guardrail whose upstream refuses the connection raises with status_code=None, and passing that through as the code crashed the proxy's error handler, which then blamed the From f640c2a5572c44d938a88239f9c8c7e4a5ffacfd Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:02:02 -0700 Subject: [PATCH 5/7] fix(proxy): pass shared-handler error messages through as on main Redacting timeout and 5xx messages changed what every shared-handler route returns, which relabeling the error type does not need, so messages go back to the verbatim text main sends. --- .../common_utils/openai_error_payload.py | 4 ---- litellm/proxy/utils.py | 11 +++------- .../proxy/utils/helpers/test_error_helpers.py | 20 ------------------- 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/litellm/proxy/common_utils/openai_error_payload.py b/litellm/proxy/common_utils/openai_error_payload.py index aea60d5217f..2fed99b31fd 100644 --- a/litellm/proxy/common_utils/openai_error_payload.py +++ b/litellm/proxy/common_utils/openai_error_payload.py @@ -51,10 +51,6 @@ def proxy_error_status_code(exc: object, message: str) -> int: return routed if routed is not None else error_status_code(exc, status.HTTP_500_INTERNAL_SERVER_ERROR) -def is_gateway_failure(status_code: int) -> bool: - return status_code == status.HTTP_408_REQUEST_TIMEOUT or status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR - - def openai_error_type_for_status(status_code: int) -> str: mapped: Final = _OPENAI_ERROR_TYPE_BY_STATUS.get(status_code) if mapped is not None: diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 1487774cd64..abf9c568ac8 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -85,7 +85,6 @@ from litellm.proxy._types import ( from litellm.proxy.bug_report_config import build_proxy_bug_report from litellm.proxy.common_utils.openai_error_payload import ( attribute_of, - is_gateway_failure, litellm_call_id_headers, openai_error_param, openai_error_type, @@ -136,7 +135,7 @@ from litellm import ( Router, ) from litellm._internal_context import service_target -from litellm._logging import redact_internal_details_from_client_message, redact_string, verbose_proxy_logger +from litellm._logging import redact_string, verbose_proxy_logger from litellm._service_logger import ServiceLogging, ServiceTypes from litellm.caching.caching import DualCache, RedisCache from litellm.caching.dual_cache import LimitedSizeOrderedDict @@ -9180,10 +9179,6 @@ def _recreate_writer_on_read_only_transaction(prisma_client: "PrismaClient | Non asyncio.create_task(prisma_client.recreate_read_only_writer(reason="postgres_read_only_transaction")) -def _client_error_message(message: str, status_code: int) -> str: - return redact_internal_details_from_client_message(message) if is_gateway_failure(status_code) else message - - def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) -> ProxyException: """ Returns an Exception as ProxyException, this ensures all exceptions are OpenAI API compatible @@ -9199,7 +9194,7 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) detail: Final = str(attribute_of(e, "detail", f"error({e})")) http_status_code: Final = proxy_error_status_code(e, detail) return ProxyException( - message=_client_error_message(detail, http_status_code), + message=detail, type=openai_error_type(e, http_status_code), param=openai_error_param(e), headers=headers, @@ -9212,7 +9207,7 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) if should_report_bug(e): verbose_proxy_logger.error(bug_report_notice(build_proxy_bug_report(e))) return ProxyException( - message=_client_error_message(message, _status_code), + message=message, type=openai_error_type(e, _status_code), param=openai_error_param(e), headers=headers, diff --git a/tests/unit/proxy/utils/helpers/test_error_helpers.py b/tests/unit/proxy/utils/helpers/test_error_helpers.py index 6f6e2dcc469..cac0c41351b 100644 --- a/tests/unit/proxy/utils/helpers/test_error_helpers.py +++ b/tests/unit/proxy/utils/helpers/test_error_helpers.py @@ -301,26 +301,6 @@ def test_handle_exception_on_proxy_keeps_the_path_a_rejected_request_names(): assert "gs://my-bucket/tmp/batch_input.jsonl" in result.message -def test_handle_exception_on_proxy_redacts_the_internal_host_an_unreachable_upstream_names(): - """A guardrail or provider the gateway cannot reach names its private address in the - error, and any virtual key can call those routes, so the host must not reach the caller.""" - result: Final = handle_exception_on_proxy(ConnectionError("Cannot connect to host 10.12.4.7:8443")) - - assert result.code == "500" - assert "Cannot connect to host" in result.message - assert "10.12.4.7" not in result.message - - -def test_handle_exception_on_proxy_redacts_the_internal_host_a_route_puts_in_a_gateway_failure_detail(): - """Routes wrap an upstream failure as an HTTPException whose detail is the raw error, so a 5xx detail carries - the same private address an unwrapped connection error does.""" - result: Final = handle_exception_on_proxy(HTTPException(status_code=503, detail="Cannot connect to host 10.0.0.9")) - - assert result.code == "503" - assert "Cannot connect to host" in result.message - assert "10.0.0.9" not in result.message - - def test_handle_exception_on_proxy_answers_500_for_an_exception_whose_status_is_none(): """A guardrail whose upstream refuses the connection raises with status_code=None, and passing that through as the code crashed the proxy's error handler, which then blamed the From fd493845521de785a27578db946bef346a0ed3bc Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:53:55 -0700 Subject: [PATCH 6/7] fix(proxy): redact only the status-less failure main never sent --- litellm/proxy/utils.py | 9 ++++++-- .../proxy/utils/helpers/test_error_helpers.py | 21 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index abf9c568ac8..3c64caf8a1e 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -135,7 +135,7 @@ from litellm import ( Router, ) from litellm._internal_context import service_target -from litellm._logging import redact_string, verbose_proxy_logger +from litellm._logging import redact_internal_details_from_client_message, redact_string, verbose_proxy_logger from litellm._service_logger import ServiceLogging, ServiceTypes from litellm.caching.caching import DualCache, RedisCache from litellm.caching.dual_cache import LimitedSizeOrderedDict @@ -9179,6 +9179,11 @@ def _recreate_writer_on_read_only_transaction(prisma_client: "PrismaClient | Non asyncio.create_task(prisma_client.recreate_read_only_writer(reason="postgres_read_only_transaction")) +def _redact_when_status_is_none(e: Exception, message: str) -> str: + status_is_none: Final = hasattr(e, "status_code") and attribute_of(e, "status_code") is None + return redact_internal_details_from_client_message(message) if status_is_none else message + + def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) -> ProxyException: """ Returns an Exception as ProxyException, this ensures all exceptions are OpenAI API compatible @@ -9207,7 +9212,7 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) if should_report_bug(e): verbose_proxy_logger.error(bug_report_notice(build_proxy_bug_report(e))) return ProxyException( - message=message, + message=_redact_when_status_is_none(e, message), type=openai_error_type(e, _status_code), param=openai_error_param(e), headers=headers, diff --git a/tests/unit/proxy/utils/helpers/test_error_helpers.py b/tests/unit/proxy/utils/helpers/test_error_helpers.py index cac0c41351b..d6c4a51f993 100644 --- a/tests/unit/proxy/utils/helpers/test_error_helpers.py +++ b/tests/unit/proxy/utils/helpers/test_error_helpers.py @@ -208,11 +208,11 @@ def test_handle_exception_on_proxy_labels_an_http_exception_by_the_status_it_ans assert (result.code, result.type) == (str(status_code), expected_type) -def _exception_carrying_status(carried: int | None) -> Exception: +def _exception_carrying_status(carried: int | None, message: str = "boom") -> Exception: class _CarriedStatus(Exception): status_code = carried - return _CarriedStatus("boom") + return _CarriedStatus(message) @pytest.mark.parametrize( @@ -301,6 +301,23 @@ def test_handle_exception_on_proxy_keeps_the_path_a_rejected_request_names(): assert "gs://my-bucket/tmp/batch_input.jsonl" in result.message +def test_handle_exception_on_proxy_redacts_the_host_an_unreachable_guardrail_names_without_a_status(): + """A guardrail that cannot reach its upstream raises with status_code=None, which crashed into a 503 before, so + its message never reached the caller and must not start handing any virtual key the guardrail's private address.""" + result: Final = handle_exception_on_proxy(_exception_carrying_status(None, "Cannot connect to host 10.12.4.7:8443")) + + assert result.code == "500" + assert "Cannot connect to host" in result.message + assert "10.12.4.7" not in result.message + + +def test_handle_exception_on_proxy_keeps_the_message_an_exception_with_a_status_carries(): + """Main sends a status-bearing failure's message verbatim, and relabeling its type must not change that text.""" + result: Final = handle_exception_on_proxy(_exception_carrying_status(503, "Cannot connect to host 10.12.4.7:8443")) + + assert (result.code, result.message) == ("503", "Cannot connect to host 10.12.4.7:8443") + + def test_handle_exception_on_proxy_answers_500_for_an_exception_whose_status_is_none(): """A guardrail whose upstream refuses the connection raises with status_code=None, and passing that through as the code crashed the proxy's error handler, which then blamed the From f4a7c264e11cf7bd9edd37d729a91f3e20a2b5a7 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:40:02 -0700 Subject: [PATCH 7/7] fix(proxy): answer a generic 500 for a status-less failure and expect auth_error on the /user/new 403 A failure with no status now answers "Internal server error" instead of its own text, since that text can name a guardrail's private host and pattern redaction misses cluster DNS names and IPv6 addresses. The /user/new integration test now expects the auth_error type main's route catch-alls give a 403 the proxy raises --- litellm/proxy/utils.py | 8 ++--- .../test_user_self_update_guard.py | 2 +- .../proxy/utils/helpers/test_error_helpers.py | 35 ++++++++++--------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 9d9ae4d069d..92b5b3db4d6 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -133,7 +133,7 @@ from litellm import ( Router, ) from litellm._internal_context import service_target -from litellm._logging import redact_internal_details_from_client_message, redact_string, verbose_proxy_logger +from litellm._logging import redact_string, verbose_proxy_logger from litellm._service_logger import ServiceLogging, ServiceTypes from litellm.caching.caching import DualCache, RedisCache from litellm.caching.dual_cache import LimitedSizeOrderedDict @@ -9177,9 +9177,9 @@ def _recreate_writer_on_read_only_transaction(prisma_client: "PrismaClient | Non asyncio.create_task(prisma_client.recreate_read_only_writer(reason="postgres_read_only_transaction")) -def _redact_when_status_is_none(e: Exception, message: str) -> str: +def _client_message(e: Exception) -> str: status_is_none: Final = hasattr(e, "status_code") and attribute_of(e, "status_code") is None - return redact_internal_details_from_client_message(message) if status_is_none else message + return "Internal server error" if status_is_none else strip_bug_report_notice(str(e)) def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) -> ProxyException: @@ -9198,7 +9198,7 @@ def handle_exception_on_proxy(e: Exception, litellm_call_id: str | None = None) return with_litellm_call_id(openai_shaped_proxy_exception(e, str(e.detail), e.status_code), litellm_call_id) if should_report_bug(e): verbose_proxy_logger.error(bug_report_notice(build_proxy_bug_report(e))) - message: Final = _redact_when_status_is_none(e, strip_bug_report_notice(str(e))) + message: Final = _client_message(e) status_code: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR) return with_litellm_call_id(openai_shaped_proxy_exception(e, message, status_code), litellm_call_id) diff --git a/tests/integration/authorization/test_user_self_update_guard.py b/tests/integration/authorization/test_user_self_update_guard.py index f33efd0141e..a4ba3d8fb6c 100644 --- a/tests/integration/authorization/test_user_self_update_guard.py +++ b/tests/integration/authorization/test_user_self_update_guard.py @@ -279,7 +279,7 @@ def test_new_user_with_admin_role_requires_proxy_admin(gateway: Gateway, role: s assert refused.json() == { "error": { "message": f"Only proxy admins can create administrative users (proxy_admin, proxy_admin_viewer). Attempted to create user with role: {role}. Your role: internal_user", - "type": "permission_error", + "type": "auth_error", "param": None, "code": "403", } diff --git a/tests/unit/proxy/utils/helpers/test_error_helpers.py b/tests/unit/proxy/utils/helpers/test_error_helpers.py index d6788f3e03f..b20857dbdaf 100644 --- a/tests/unit/proxy/utils/helpers/test_error_helpers.py +++ b/tests/unit/proxy/utils/helpers/test_error_helpers.py @@ -324,16 +324,6 @@ def test_handle_exception_on_proxy_keeps_the_path_a_rejected_request_names(): assert "gs://my-bucket/tmp/batch_input.jsonl" in result.message -def test_handle_exception_on_proxy_redacts_the_host_an_unreachable_guardrail_names_without_a_status(): - """A guardrail that cannot reach its upstream raises with status_code=None, which crashed into a 503 before, so - its message never reached the caller and must not start handing any virtual key the guardrail's private address.""" - result: Final = handle_exception_on_proxy(_exception_carrying_status(None, "Cannot connect to host 10.12.4.7:8443")) - - assert result.code == "500" - assert "Cannot connect to host" in result.message - assert "10.12.4.7" not in result.message - - def test_handle_exception_on_proxy_keeps_the_message_an_exception_with_a_status_carries(): """Main sends a status-bearing failure's message verbatim, and relabeling its type must not change that text.""" result: Final = handle_exception_on_proxy(_exception_carrying_status(503, "Cannot connect to host 10.12.4.7:8443")) @@ -341,13 +331,26 @@ def test_handle_exception_on_proxy_keeps_the_message_an_exception_with_a_status_ assert (result.code, result.message) == ("503", "Cannot connect to host 10.12.4.7:8443") -def test_handle_exception_on_proxy_answers_500_for_an_exception_whose_status_is_none(): - """A guardrail whose upstream refuses the connection raises with status_code=None, and - passing that through as the code crashed the proxy's error handler, which then blamed the - auth database with a 503 instead of naming the unreachable guardrail.""" - result: Final = handle_exception_on_proxy(_exception_carrying_status(None)) +@pytest.mark.parametrize( + "message", + [ + "boom", + "Cannot connect to host 10.12.4.7:8443", + "Cannot connect to host guardrail.internal.svc.cluster.local:443", + "Cannot connect to host [fd00::12]:8443", + ], + ids=["plain", "ipv4", "cluster_dns", "ipv6"], +) +def test_handle_exception_on_proxy_answers_a_generic_500_for_an_exception_whose_status_is_none(message: str): + """A guardrail whose upstream refuses the connection raises with status_code=None, which crashed the handler + into a 503 blaming the auth database, and its text names private hosts no virtual key was ever sent.""" + result: Final = handle_exception_on_proxy(_exception_carrying_status(None, message)) - assert (result.code, result.type, result.message) == ("500", ProxyErrorTypes.internal_server_error.value, "boom") + assert (result.code, result.type, result.message) == ( + "500", + ProxyErrorTypes.internal_server_error.value, + "Internal server error", + ) @pytest.mark.parametrize(