Skip to content

Commit

Permalink
refactor: move datasource error catching
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jun 25, 2020
1 parent b69d4f2 commit 7902ba0
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions lib/datasource/index.ts
Expand Up @@ -63,16 +63,7 @@ async function firstRegistry(
);
}
const registryUrl = registryUrls[0];
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;
}
return getRegistryReleases(datasource, config, registryUrl);
}

async function huntRegistries(
Expand All @@ -81,7 +72,7 @@ async function huntRegistries(
registryUrls: string[]
): Promise<ReleaseResult> {
let res: ReleaseResult;
let datasourceError;
let caughtError;
for (const registryUrl of registryUrls) {
try {
res = await getRegistryReleases(datasource, config, registryUrl);
Expand All @@ -93,15 +84,15 @@ async function huntRegistries(
throw err;
}
// We'll always save the last-thrown error
datasourceError = err;
caughtError = err;
logger.trace({ err }, 'datasource hunt failure');
}
}
if (res) {
return res;
}
if (datasourceError) {
logError(datasource.id, config.lookupName, datasourceError);
if (caughtError) {
throw caughtError;
}
return null;
}
Expand All @@ -112,7 +103,7 @@ async function mergeRegistries(
registryUrls: string[]
): Promise<ReleaseResult> {
let combinedRes: ReleaseResult;
let datasourceError;
let caughtError;
for (const registryUrl of registryUrls) {
try {
const res = await getRegistryReleases(datasource, config, registryUrl);
Expand All @@ -127,7 +118,7 @@ async function mergeRegistries(
throw err;
}
// We'll always save the last-thrown error
datasourceError = err;
caughtError = err;
logger.trace({ err }, 'datasource merge failure');
}
}
Expand All @@ -145,8 +136,8 @@ async function mergeRegistries(
if (combinedRes) {
return combinedRes;
}
if (datasourceError) {
logError(datasource.id, config.lookupName, datasourceError);
if (caughtError) {
throw caughtError;
}
return null;
}
Expand Down Expand Up @@ -176,28 +167,35 @@ async function fetchReleases(
}
const datasource = await load(datasourceName);
const registryUrls = resolveRegistryUrls(datasource, config.registryUrls);
let dep: ReleaseResult;
if (datasource.registryStrategy) {
// istanbul ignore if
if (!registryUrls.length) {
logger.warn(
{ datasource: datasourceName, depName: config.depName },
'Missing registryUrls for registryStrategy'
);
return null;
let dep: ReleaseResult = null;
try {
if (datasource.registryStrategy) {
// istanbul ignore if
if (!registryUrls.length) {
logger.warn(
{ datasource: datasourceName, depName: config.depName },
'Missing registryUrls for registryStrategy'
);
return null;
}
if (datasource.registryStrategy === 'first') {
dep = await firstRegistry(config, datasource, registryUrls);
} else if (datasource.registryStrategy === 'hunt') {
dep = await huntRegistries(config, datasource, registryUrls);
} else if (datasource.registryStrategy === 'merge') {
dep = await mergeRegistries(config, datasource, registryUrls);
}
} else {
dep = await datasource.getReleases({
...config,
registryUrls,
});
}
if (datasource.registryStrategy === 'first') {
dep = await firstRegistry(config, datasource, registryUrls);
} else if (datasource.registryStrategy === 'hunt') {
dep = await huntRegistries(config, datasource, registryUrls);
} else if (datasource.registryStrategy === 'merge') {
dep = await mergeRegistries(config, datasource, registryUrls);
} catch (err) {
if (err instanceof ExternalHostError) {
throw err;
}
} else {
dep = await datasource.getReleases({
...config,
registryUrls,
});
logError(datasource.id, config.lookupName, err);
}
if (!dep || _.isEqual(dep, { releases: [] })) {
return null;
Expand Down

0 comments on commit 7902ba0

Please sign in to comment.