fix(autoUpdate): improve error handling and UI reset during update process

This commit is contained in:
MasterAcnolo
2026-09-29 17:20:42 +02:00
parent ac1d74ff23
commit 1937738ab8
3 changed files with 31 additions and 9 deletions

View File

@@ -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;
}
}

View File

@@ -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)),

View File

@@ -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();
}
};