fix(vertex): SA 认证注入代理配置,细化 token 获取异常处理

- _auth_service_account 接收 endpoint 参数,通过 _get_proxy_config 解析代理
- vertex_auth 区分 TimeoutException/RequestError/通用异常,提供可读错误信息
- 新增测试覆盖代理传递和超时场景
This commit is contained in:
fawney19
2026-03-20 00:49:44 +08:00
parent 28fa03451c
commit 772f2ea601
5 changed files with 184 additions and 4 deletions
+9 -1
View File
@@ -174,8 +174,16 @@ class VertexAuthService:
raise VertexAuthError(
f"Failed to get access token: HTTP {e.response.status_code}: {error_body}"
)
except httpx.TimeoutException:
raw = client_kwargs.get("timeout")
suffix = f" after {raw}s" if isinstance(raw, (int, float)) else ""
raise VertexAuthError(f"Failed to get access token: request timed out{suffix}")
except httpx.RequestError as e:
detail = str(e).strip() or type(e).__name__
raise VertexAuthError(f"Failed to get access token: {detail}")
except Exception as e:
raise VertexAuthError(f"Failed to get access token: {e}")
detail = str(e).strip() or type(e).__name__
raise VertexAuthError(f"Failed to get access token: {detail}")
@classmethod
def clear_cache(cls, client_email: str | None = None) -> None:
@@ -12,7 +12,7 @@ from typing import Any
from src.core.provider_auth_types import ProviderAuthInfo
async def _auth_service_account(key: Any) -> ProviderAuthInfo:
async def _auth_service_account(key: Any, endpoint: Any | None = None) -> ProviderAuthInfo:
"""Service Account 认证:SA JSON → JWT → Access Token。"""
from src.core.crypto import crypto_service
from src.core.exceptions import InvalidRequestException
@@ -38,11 +38,14 @@ async def _auth_service_account(key: Any) -> ProviderAuthInfo:
raise InvalidRequestException("Service Account JSON 无效,请重新添加该密钥。")
# 获取 Access Token(注入代理配置)
from src.services.provider.auth import _get_proxy_config
from src.services.proxy_node.resolver import build_proxy_client_kwargs
effective_proxy = _get_proxy_config(key, endpoint)
service = VertexAuthService(sa_json)
access_token = await service.get_access_token(
httpx_client_kwargs=build_proxy_client_kwargs(timeout=30),
httpx_client_kwargs=build_proxy_client_kwargs(effective_proxy, timeout=30),
)
return ProviderAuthInfo(
+1 -1
View File
@@ -556,7 +556,7 @@ async def get_provider_auth(
# "vertex_ai" 保留为向后兼容(迁移期间旧数据可能仍使用该值)
from src.services.provider.adapters.vertex_ai.auth import _auth_service_account
return await _auth_service_account(key)
return await _auth_service_account(key, endpoint)
# 标准 API Key:返回 None,由 build_headers 处理
return None