Skip to content

Commit

Permalink
fix: gracefully handle version download failure (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored Mar 3, 2024
1 parent 8b9c116 commit d577648
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/renderer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,17 +931,30 @@ export class AppState {
* @returns {Promise<void>}
*/
public async setVersion(input: string): Promise<void> {
// 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;

Expand All @@ -959,9 +972,6 @@ export class AppState {
await window.app.replaceFiddle(values, options);
}
}

// Fetch new binaries, maybe?
await this.downloadVersion(ver);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/renderer/state-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d577648

Please sign in to comment.