From d577648ef4a48ea36b9f393882a89d1f024eb8e2 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sun, 3 Mar 2024 21:53:01 +0100 Subject: [PATCH] fix: gracefully handle version download failure (#1554) --- src/renderer/state.ts | 22 ++++++++++++++++------ tests/renderer/state-spec.ts | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/renderer/state.ts b/src/renderer/state.ts index e5568a28a0..71b3addba2 100644 --- a/src/renderer/state.ts +++ b/src/renderer/state.ts @@ -931,17 +931,30 @@ export class AppState { * @returns {Promise} */ public async setVersion(input: string): Promise { - // make sure we can use this version + const fallback = this.findUsableVersion(); + const { err, ver } = this.isVersionUsable(input); if (!ver) { - console.warn(`setVersion('${input}') failed: ${err}`); + console.error(`setVersion('${input}') failed: ${err}`); this.showErrorDialog(err!); - const fallback = this.findUsableVersion(); if (fallback) await this.setVersion(fallback.version); return; } const { version } = ver; + + try { + await this.downloadVersion(ver); + } catch { + await this.removeVersion(ver); + console.error( + `setVersion('${input}') failed: Couldn't download ${version}`, + ); + this.showErrorDialog(`Failed to download Electron version ${version}`); + if (fallback) await this.setVersion(fallback.version); + return; + } + console.log(`State: Switching to Electron ${version}`); this.version = version; @@ -959,9 +972,6 @@ export class AppState { await window.app.replaceFiddle(values, options); } } - - // Fetch new binaries, maybe? - await this.downloadVersion(ver); } /** diff --git a/tests/renderer/state-spec.ts b/tests/renderer/state-spec.ts index c3f695863f..d9856900b3 100644 --- a/tests/renderer/state-spec.ts +++ b/tests/renderer/state-spec.ts @@ -406,6 +406,23 @@ describe('AppState', () => { expect(appState.downloadVersion).toHaveBeenCalled(); }); + it('falls back if downloading the new version fails', async () => { + appState.downloadVersion = jest + .fn() + .mockRejectedValueOnce(new Error('FAILURE')); + appState.showGenericDialog = jest.fn().mockResolvedValueOnce({ + confirm: true, + }); + + await appState.setVersion('v2.0.2'); + expect(appState.showGenericDialog).toHaveBeenCalledWith({ + label: 'Failed to download Electron version 2.0.2', + ok: 'Close', + type: GenericDialogType.warning, + wantsInput: false, + }); + }); + describe('loads the template for the new version', () => { let newVersion: string; let oldVersion: string;