@@ -107,6 +107,7 @@ export async function applyOtaUpdate(onProgress) {
|
||||
|
||||
// 注册一次性进度监听 (applyUpdate 期间触发)
|
||||
let unlisten = null
|
||||
let unlistenLifecycle = null
|
||||
if (onProgress) {
|
||||
try {
|
||||
unlisten = await mod.onDownloadProgress((p) => onProgress(p.downloaded, p.total))
|
||||
@@ -115,13 +116,39 @@ export async function applyOtaUpdate(onProgress) {
|
||||
}
|
||||
}
|
||||
|
||||
// 监听 lifecycle 事件, 捕获失败详情 (download-error / apply 失败时 error 字段带具体原因)
|
||||
// applyUpdate() 抛出的错误往往只有泛化信息, 细节藏在 lifecycle 事件里
|
||||
let lifecycleError = ''
|
||||
try {
|
||||
unlistenLifecycle = await mod.onLifecycle((ev) => {
|
||||
if (ev.event === 'download-error' || ev.event === 'apply' || ev.event === 'check-error') {
|
||||
if (ev.error) {
|
||||
lifecycleError = ev.error
|
||||
console.error('[ota] lifecycle', ev.event, 'error:', ev.error)
|
||||
}
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.warn('[ota] onLifecycle 监听失败:', e)
|
||||
}
|
||||
|
||||
try {
|
||||
const newVersion = await mod.applyUpdate()
|
||||
return newVersion
|
||||
} catch (e) {
|
||||
// 把 lifecycle 里的详细错误拼进抛出的异常, 方便前端展示真实原因
|
||||
const base = e?.message || '升级失败'
|
||||
const detail = lifecycleError ? ` (${lifecycleError})` : ''
|
||||
const err = new Error(base + detail)
|
||||
err.cause = e
|
||||
throw err
|
||||
} finally {
|
||||
if (unlisten) {
|
||||
try { unlisten() } catch { /* ignore */ }
|
||||
}
|
||||
if (unlistenLifecycle) {
|
||||
try { unlistenLifecycle() } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user