fix: Improve error handling in configuration load

- Added conditional check to prevent error when modelArray is empty
- Moved axios request into try-catch block to handle network errors
- Modified refreshModelList to handle fetchLLMs errors gracefully
This commit is contained in:
bobo 2024-05-08 18:35:34 +08:00
parent 58c2708a7d
commit fa23fa8dc8
2 changed files with 12 additions and 7 deletions

View File

@ -119,7 +119,9 @@ const Config = observer(() => {
label: getModelShowName(item.name),
}));
setModels(modelArray);
setCurrent(modelArray[0].value);
if (modelArray.length > 0) {
setCurrent(modelArray[0].value);
}
}, [router.currentRoute]);
useEffect(() => {
const cloneConfig = cloneDeep(config.config);

View File

@ -12,17 +12,16 @@ const defaultAPIBase = [
export const fetchLLMs = async ({modelsUrl,devchatApiKey}) => {
return new Promise<{data:any}>((resolve, reject) => {
try {
// 添加 header: "Authorization: Bearer ${devchatApiKey}"
axios.get(`${modelsUrl}/models`, { headers: { 'Authorization': `Bearer ${devchatApiKey}` }}).then((res) => {
// 获取 models 模版列表
if (res?.data?.data && Array.isArray(res?.data?.data)) {
resolve(res.data);
}
}).catch((e) => {
console.error("fetchLLMs error:", e);
reject(e);
});
} catch (e) {
reject(e);
}
}
);
};
@ -180,8 +179,12 @@ export const ConfigStore = types
this.updateSettle(true);
},
refreshModelList: flow(function* (){
const { data } = yield fetchLLMs({modelsUrl:self.modelsUrl,devchatApiKey:self.devchatApiKey});
setTemplate(data,self.provider);
try {
const { data } = yield fetchLLMs({modelsUrl:self.modelsUrl,devchatApiKey:self.devchatApiKey});
setTemplate(data,self.provider);
} catch (e) {
console.log("fetchLLMs error:", e);
}
}),
writeConfig: function () {
const writeConfig = cloneDeep(self.config);