Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor: add try/catch and logging to datasource wrapper
  • Loading branch information
rarkins committed Jun 25, 2020
1 parent 55625a8 commit b69d4f2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
16 changes: 8 additions & 8 deletions lib/datasource/index.spec.ts
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
47 changes: 36 additions & 11 deletions lib/datasource/index.ts
Expand Up @@ -30,6 +30,18 @@ function load(datasource: string): Promise<Datasource> {

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,
Expand All @@ -39,7 +51,7 @@ async function getRegistryReleases(
return res;
}

function firstRegistry(
async function firstRegistry(
config: GetReleasesInternalConfig,
datasource: Datasource,
registryUrls: string[]
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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<string>();
Expand All @@ -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(
Expand Down

0 comments on commit b69d4f2

Please sign in to comment.