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: don't return empty ElectronVersions on fetch failures #117

Merged
merged 1 commit into from
Sep 6, 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
8 changes: 8 additions & 0 deletions src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ export class ElectronVersions extends BaseVersions {
const url = 'https://releases.electronjs.org/releases.json';
d('fetching releases list from', url);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Fetching versions failed with status code: ${response.status}`,
);
}
const json = (await response.json()) as unknown;
await fs.outputJson(cacheFile, json);
return json;
Expand Down Expand Up @@ -329,6 +334,9 @@ export class ElectronVersions extends BaseVersions {
versions = await ElectronVersions.fetchVersions(versionsCache);
} catch (err) {
d('error fetching versions', err);
if (!versions) {
throw err;
}
}
}

Expand Down
20 changes: 17 additions & 3 deletions tests/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,26 @@ describe('ElectronVersions', () => {
expect(versions.length).toBe(2);
});

it('has no versions with a missing cache and failed fetch', async () => {
it('throws an error with a missing cache and failed fetch', async () => {
const scope = nockScope.get('/releases.json').replyWithError('Error');
await fs.remove(versionsCache);
const { versions } = await ElectronVersions.create({ versionsCache });
await expect(ElectronVersions.create({ versionsCache })).rejects.toThrow(
Error,
);
expect(scope.isDone());
});

it('throws an error with a missing cache and a non-200 server response', async () => {
const scope = nockScope
.get('/releases.json')
.reply(500, JSON.stringify({ error: true }), {
'Content-Type': 'application/json',
});
await fs.remove(versionsCache);
await expect(ElectronVersions.create({ versionsCache })).rejects.toThrow(
Error,
);
expect(scope.isDone());
expect(versions.length).toBe(0);
});

it('fetches with a stale cache', async () => {
Expand Down