Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully handle version download failure #1554

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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