feat:修改模型列表获取错误处理,统一使用502状态码响应

pull/1385/head
墨梓柒 2025-11-26 23:02:04 +08:00
parent 0a0fb8ee55
commit 2f87519faa
No known key found for this signature in database
GPG Key ID: 4A65B9DBA35F7635
1 changed files with 7 additions and 5 deletions

View File

@ -121,16 +121,18 @@ async def _fetch_models_from_provider(
except httpx.TimeoutException:
raise HTTPException(status_code=504, detail="请求超时,请稍后重试")
except httpx.HTTPStatusError as e:
# 注意:使用 502 Bad Gateway 而不是原始的 401/403
# 因为前端的 fetchWithAuth 会把 401 当作 WebUI 认证失败处理
if e.response.status_code == 401:
raise HTTPException(status_code=401, detail="API Key 无效或已过期")
raise HTTPException(status_code=502, detail="API Key 无效或已过期")
elif e.response.status_code == 403:
raise HTTPException(status_code=403, detail="没有权限访问模型列表")
raise HTTPException(status_code=502, detail="没有权限访问模型列表,请检查 API Key 权限")
elif e.response.status_code == 404:
raise HTTPException(status_code=404, detail="该提供商不支持获取模型列表")
raise HTTPException(status_code=502, detail="该提供商不支持获取模型列表")
else:
raise HTTPException(
status_code=e.response.status_code,
detail=f"请求失败: {e.response.text}"
status_code=502,
detail=f"上游服务请求失败 ({e.response.status_code}): {e.response.text[:200]}"
)
except Exception as e:
logger.error(f"获取模型列表失败: {e}")