diff --git a/src/services/version.js b/src/services/version.js index 67299664..6367effc 100644 --- a/src/services/version.js +++ b/src/services/version.js @@ -28,7 +28,19 @@ export const checkForUpdate = () => new Promise(async (resolve, reject) => { const res = await fetch(UPDATE_URL) const data = [] const extract = tar.extract() - if (res.status === 404) { return resolve({ available: false }) } + + if (res.status === 404) { + return resolve({ available: false }) + } + + if (res.status >= 500) { + if (process.env.DEBUG) { + console.log(chalk.yellow(`Warning: The update server (${UPDATE_URL}) responded with a ${res.status} status code\n`)) + } + + return resolve({ available: false }) + } + Readable.fromWeb(res.body).pipe(createGunzip()).pipe(extract) extract.on('entry', (header, stream, next) => { diff --git a/test/services/version.test.js b/test/services/version.test.js new file mode 100644 index 00000000..ed4c7d9d --- /dev/null +++ b/test/services/version.test.js @@ -0,0 +1,40 @@ +import { mock, test } from 'node:test' +import * as assert from 'node:assert' +import { checkForUpdate } from '../../src/services/version.js' +import { STATUS_CODES } from 'node:http' + +test('checkForUpdate() handles 404 response', async (context) => { + fetch = context.mock.fn(fetch, () => { + return { + status: 404 + } + }) + + const actual = await checkForUpdate() + assert.deepEqual(actual, { available: false }) +}) + +test('checkForUpdate() handles 5xx responses', async (context) => { + const statusCodes = Object + .keys(STATUS_CODES) + .filter((statusCode) => +statusCode >= 500 ) + .map((statusCode) => +statusCode) + + const statusContexts = statusCodes.map((statusCode) => { + return { + status: statusCode, + expected: { available: false }, + } + }); + + for (const statusContext of statusContexts) { + fetch = context.mock.fn(fetch, () => { + return { + status: statusContext.status, + } + }) + + const actual = await checkForUpdate() + assert.deepEqual(actual, statusContext.expected) + } +})