From b69d4f29e5ad4153be15a928ed3820a17d2cd07b Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Thu, 25 Jun 2020 09:34:41 +0200 Subject: [PATCH] refactor: add try/catch and logging to datasource wrapper --- lib/datasource/index.spec.ts | 16 ++++++------ lib/datasource/index.ts | 47 +++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/lib/datasource/index.spec.ts b/lib/datasource/index.spec.ts index 160a5bb30f50a0..cce29d3bfb255b 100644 --- a/lib/datasource/index.spec.ts +++ b/lib/datasource/index.spec.ts @@ -143,20 +143,20 @@ describe('datasource/index', () => { }) ).rejects.toThrow(EXTERNAL_HOST_ERROR); }); - it('hunts registries and passes on error', async () => { + it('hunts registries and returns null', async () => { packagistDatasource.getReleases.mockImplementationOnce(() => { throw new Error('a'); }); packagistDatasource.getReleases.mockImplementationOnce(() => { throw new Error('b'); }); - await expect( - datasource.getPkgReleases({ + expect( + await datasource.getPkgReleases({ datasource: datasourcePackagist.id, depName: 'something', registryUrls: ['https://reg1.com', 'https://reg2.io'], }) - ).rejects.toThrow('b'); + ).toBeNull(); }); it('merges registries and returns success', async () => { mavenDatasource.getReleases.mockResolvedValueOnce({ @@ -185,20 +185,20 @@ describe('datasource/index', () => { }) ).rejects.toThrow(EXTERNAL_HOST_ERROR); }); - it('merges registries and passes on error', async () => { + it('merges registries and returns null for error', async () => { mavenDatasource.getReleases.mockImplementationOnce(() => { throw new Error('a'); }); mavenDatasource.getReleases.mockImplementationOnce(() => { throw new Error('b'); }); - await expect( - datasource.getPkgReleases({ + expect( + await datasource.getPkgReleases({ datasource: datasourceMaven.id, depName: 'something', registryUrls: ['https://reg1.com', 'https://reg2.io'], }) - ).rejects.toThrow('b'); + ).toBeNull(); }); it('trims sourceUrl', async () => { npmDatasource.getReleases.mockResolvedValue({ diff --git a/lib/datasource/index.ts b/lib/datasource/index.ts index 92a5d9ac6df134..bc3fe6d73bcb2a 100644 --- a/lib/datasource/index.ts +++ b/lib/datasource/index.ts @@ -30,6 +30,18 @@ function load(datasource: string): Promise { type GetReleasesInternalConfig = GetReleasesConfig & GetPkgReleasesConfig; +// istanbul ignore next +function logError(datasource, lookupName, err): void { + const { statusCode, url } = err; + if (statusCode === 404) { + logger.debug({ datasource, lookupName, url }, 'Datasource 404'); + } else if (statusCode === 401 || statusCode === 403) { + logger.debug({ datasource, lookupName, url }, 'Datasource unauthorized'); + } else { + logger.debug({ datasource, lookupName, err }, 'Datasource unknown error'); + } +} + async function getRegistryReleases( datasource, config: GetReleasesConfig, @@ -39,7 +51,7 @@ async function getRegistryReleases( return res; } -function firstRegistry( +async function firstRegistry( config: GetReleasesInternalConfig, datasource: Datasource, registryUrls: string[] @@ -51,7 +63,16 @@ function firstRegistry( ); } const registryUrl = registryUrls[0]; - return getRegistryReleases(datasource, config, registryUrl); + try { + const res = await getRegistryReleases(datasource, config, registryUrl); + return res; + } catch (err) /* istanbul ignore next */ { + if (err instanceof ExternalHostError) { + throw err; + } + logError(datasource.id, config.lookupName, err); + return null; + } } async function huntRegistries( @@ -76,11 +97,13 @@ async function huntRegistries( logger.trace({ err }, 'datasource hunt failure'); } } - if (res === undefined && datasourceError) { - // if we failed to get a result and also got an error then throw it - throw datasourceError; + if (res) { + return res; + } + if (datasourceError) { + logError(datasource.id, config.lookupName, datasourceError); } - return res; + return null; } async function mergeRegistries( @@ -108,10 +131,6 @@ async function mergeRegistries( logger.trace({ err }, 'datasource merge failure'); } } - if (combinedRes === undefined && datasourceError) { - // if we failed to get a result and also got an error then throw it - throw datasourceError; - } // De-duplicate releases if (combinedRes?.releases?.length) { const seenVersions = new Set(); @@ -123,7 +142,13 @@ async function mergeRegistries( return true; }); } - return combinedRes; + if (combinedRes) { + return combinedRes; + } + if (datasourceError) { + logError(datasource.id, config.lookupName, datasourceError); + } + return null; } function resolveRegistryUrls(