From 1937738ab85e8526b5602332b51a562470b49c4b Mon Sep 17 00:00:00 2001 From: MasterAcnolo Date: Tue, 29 Sep 2026 17:20:42 +0200 Subject: [PATCH] fix(autoUpdate): improve error handling and UI reset during update process --- app/autoUpdater.js | 4 ++++ preload.js | 9 +++++++++ public/script/autoUpdate.js | 27 ++++++++++++++++++--------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/autoUpdater.js b/app/autoUpdater.js index 3b840f3..d3abab8 100644 --- a/app/autoUpdater.js +++ b/app/autoUpdater.js @@ -112,6 +112,7 @@ function initAutoUpdater(mainWindow) { } logger.error("Auto update error:", err); + mainWindow?.webContents.send("update-error", msg); }); checkForUpdates(); @@ -122,6 +123,8 @@ function initAutoUpdater(mainWindow) { * Separated from init for reusability and testability. */ async function checkForUpdates() { + + // If app is was installed from SNAP or Flatpak. if (process.env.SNAP || process.env.FLATPAK_ID) return; if (!require("electron").app.isPackaged) return; @@ -141,6 +144,7 @@ async function downloadUpdate() { await autoUpdater.downloadUpdate(); } catch (err) { logger.error("Download failed:", err.message); + throw err; } } diff --git a/preload.js b/preload.js index 82363c8..b05d6a6 100644 --- a/preload.js +++ b/preload.js @@ -122,10 +122,19 @@ contextBridge.exposeInMainWorld("electronAPI", { */ installUpdate: () => ipcRenderer.invoke("install-update"), + /** + * Subscribes to the update error event. + * + * @param {Function} callback - Function executed with error information when an update error occurs. + * @returns {Electron.IpcRenderer} + */ + onUpdateError: (callback) => ipcRenderer.on("update-error", (_, err) => callback(err)), + /** * Subscribes to the download progress event. * * @param {Function} callback - Function executed with progress metrics (percent, bytesPerSecond, transferred, total). + * @returns {Electron.IpcRenderer} */ onDownloadProgress: (callback) => ipcRenderer.on("update-progress", (_, progress) => callback(progress)), diff --git a/public/script/autoUpdate.js b/public/script/autoUpdate.js index 0cced57..d11271a 100644 --- a/public/script/autoUpdate.js +++ b/public/script/autoUpdate.js @@ -50,6 +50,23 @@ async function initUpdateUI() { const initialTitleText = title.textContent; const initialSubText = sub.textContent; + const resetUI = () => { + btn.disabled = false; + btn.textContent = initialButtonText; + icon.textContent = initialIconText; + title.textContent = initialTitleText; + sub.textContent = initialSubText; + progressWrap.style.display = "none"; + progressFill.style.width = "0%"; + progressPct.textContent = "0%"; + progressEta.textContent = ""; + }; + + window.electronAPI.onUpdateError((err) => { + console.error("Update failed asynchronously:", err); + resetUI(); + }); + btn.onclick = async () => { btn.disabled = true; @@ -61,15 +78,7 @@ async function initUpdateUI() { try { await window.electronAPI.downloadUpdate(); } catch { - btn.disabled = false; - btn.textContent = initialButtonText; - icon.textContent = initialIconText; - title.textContent = initialTitleText; - sub.textContent = initialSubText; - progressWrap.style.display = "none"; - progressFill.style.width = "0%"; - progressPct.textContent = "0%"; - progressEta.textContent = ""; + resetUI(); } };