添加配置数据验证失败的错误日志记录,并处理已删除提供商的模型引用问题

pull/1432/head
墨梓柒 2025-12-13 18:38:53 +08:00
parent e680a4d1f5
commit 5cd15cdd1e
No known key found for this signature in database
GPG Key ID: 4A65B9DBA35F7635
1 changed files with 12 additions and 0 deletions

View File

@ -368,6 +368,18 @@ async def update_model_config_section(section_name: str, section_data: SectionBo
try:
APIAdapterConfig.from_dict(config_data)
except Exception as e:
logger.error(f"配置数据验证失败,详细错误: {str(e)}")
# 特殊处理:如果是更新 api_providers检查是否有模型引用了已删除的provider
if section_name == "api_providers" and "api_provider" in str(e):
provider_names = {p.get("name") for p in section_data if isinstance(p, dict)}
models = config_data.get("models", [])
orphaned_models = [
m.get("name") for m in models
if isinstance(m, dict) and m.get("api_provider") not in provider_names
]
if orphaned_models:
error_msg = f"以下模型引用了已删除的提供商: {', '.join(orphaned_models)}。请先在模型管理页面删除这些模型,或重新分配它们的提供商。"
raise HTTPException(status_code=400, detail=error_msg) from e
raise HTTPException(status_code=400, detail=f"配置数据验证失败: {str(e)}") from e
# 保存配置(格式化数组为多行,保留注释)