fix(form): 避免 Select init 请求失败或路径缺失时抛出未处理异常

This commit is contained in:
roymondchen 2026-09-04 11:27:06 +08:00
parent 092bc2914d
commit d1cb8c3853
2 changed files with 24 additions and 3 deletions

View File

@ -372,7 +372,12 @@ const getInitOption = async () => {
});
}
let initData = getValueByKeyPath(initRoot || root, res);
let initData: any;
try {
initData = getValueByKeyPath(initRoot || root, res);
} catch {
// initRoot/root
}
if (initData) {
if (!Array.isArray(initData)) {
initData = [initData];
@ -417,8 +422,12 @@ if (typeof props.config.options === 'function') {
const v = props.model[props.name];
if (Array.isArray(v) ? !v.length : typeof v === 'undefined') return;
if (typeof v === 'undefined' || hasOption(v)) return;
const data = await getInitOption();
setOptions(data);
try {
const data = await getInitOption();
setOptions(data);
} catch {
setOptions([]);
}
};
watch(

View File

@ -691,4 +691,16 @@ describe('Select - getInitOption 初始化接口分支', () => {
expect((wrapper.findComponent(MSelect).vm as any).options).toEqual([]);
});
test('init 请求失败时 options 为空且不抛未处理异常', async () => {
request = vi.fn(async () => {
throw new Error('init-fail');
});
setConfig({ request });
const wrapper = mountFormWithRequest({ initUrl: 'https://example.com/init', initRoot: 'data.obj' }, { s: 'a' });
await flushAsync();
expect((wrapper.findComponent(MSelect).vm as any).options).toEqual([]);
});
});