diff --git a/src/versions.ts b/src/versions.ts index 8448fff..7f437aa 100644 --- a/src/versions.ts +++ b/src/versions.ts @@ -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; @@ -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; + } } } diff --git a/tests/versions.test.ts b/tests/versions.test.ts index 678eb11..a3e24a0 100644 --- a/tests/versions.test.ts +++ b/tests/versions.test.ts @@ -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 () => {