Skip to content

Commit

Permalink
fix: don't return empty ElectronVersions on fetch failures (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Sep 6, 2024
1 parent d6e8a6a commit c3c171b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit c3c171b

Please sign in to comment.