Skip to content

Commit 7aa3ada

Browse files
authored
Merge pull request #1684 from IdsTeepe/fix/null-checks
Fix: Null checks, more fetch error handling
2 parents 2d62f87 + c5fed67 commit 7aa3ada

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

dist/tools/libs/tools.mjs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,17 @@ class DotnetTool {
336336
async getQueryServices() {
337337
const builder = new ArgumentsBuilder().addArgument("nuget").addArgument("list").addArgument("source").addKeyValue("format", "short");
338338
const result = await this.execute("dotnet", builder.build());
339-
const nugetSources = [...(result.stdout ?? "").matchAll(/^E (?<index>.+)/gm)].map((m) => m.groups.index);
339+
const nugetSources = [...(result.stdout ?? "").matchAll(/^E (?<index>.+)/gm)].map((m) => m.groups?.index ?? "").filter((s) => !!s);
340340
if (!nugetSources.length) {
341341
this.buildAgent.error("Failed to fetch an enabled package source for dotnet.");
342342
return [];
343343
}
344344
const sources = [];
345345
for (const nugetSource of nugetSources) {
346-
const nugetIndex = await fetch(nugetSource);
346+
const nugetIndex = await fetch(nugetSource).catch((e) => {
347+
this.buildAgent.warn(e.cause?.message ?? "An unknown error occurred while fetching data");
348+
return Response.error();
349+
});
347350
if (!nugetIndex?.ok) {
348351
this.buildAgent.warn(`Failed to fetch data from NuGet source ${nugetSource}.`);
349352
continue;
@@ -359,17 +362,22 @@ class DotnetTool {
359362
return sources;
360363
}
361364
async queryVersionsFromNugetSource(serviceUrl, toolName, includePrerelease) {
365+
this.buildAgent.debug(`Fetching ${toolName} versions from source ${serviceUrl}`);
362366
const toolNameParam = encodeURIComponent(toolName.toLowerCase());
363367
const prereleaseParam = includePrerelease ? "true" : "false";
364368
const downloadPath = `${serviceUrl}?q=${toolNameParam}&prerelease=${prereleaseParam}&semVerLevel=2.0.0&take=1`;
365-
const response = await fetch(downloadPath);
369+
const response = await fetch(downloadPath).catch((e) => {
370+
this.buildAgent.warn(e.cause?.message ?? "An unknown error occurred while fetching data");
371+
return Response.error();
372+
});
366373
if (!response || !response.ok) {
367374
this.buildAgent.warn(`failed to query latest version for ${toolName} from ${downloadPath}. Status code: ${response ? response.status : "unknown"}`);
368375
return [];
369376
}
370377
const { data } = await response.json();
371-
const versions = data[0].versions.map((x) => x.version);
372-
return versions ?? [];
378+
const versions = data?.[0]?.versions?.map((x) => x.version) ?? [];
379+
this.buildAgent.debug(`Found ${versions.length} versions: ${versions.join(", ")}`);
380+
return versions;
373381
}
374382
async queryLatestMatch(toolName, versionSpec, includePrerelease) {
375383
this.buildAgent.info(

dist/tools/libs/tools.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/common/dotnet-tool.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export abstract class DotnetTool implements IDotnetTool {
232232
const result = await this.execute('dotnet', builder.build())
233233

234234
// Each line of the output starts with either E (enabled) or D (disabled), followed by a space and index url.
235-
const nugetSources = [...(result.stdout ?? '').matchAll(/^E (?<index>.+)/gm)].map(m => m.groups!.index)
235+
const nugetSources = [...(result.stdout ?? '').matchAll(/^E (?<index>.+)/gm)].map(m => m.groups?.index ?? '').filter(s => !!s)
236236

237237
if (!nugetSources.length) {
238238
this.buildAgent.error('Failed to fetch an enabled package source for dotnet.')
@@ -242,7 +242,10 @@ export abstract class DotnetTool implements IDotnetTool {
242242
const sources: string[] = []
243243
for (const nugetSource of nugetSources) {
244244
// Fetch the nuget source index to obtain the query service
245-
const nugetIndex = await fetch(nugetSource)
245+
const nugetIndex = await fetch(nugetSource).catch((e: { cause: { message: string | undefined } | undefined }) => {
246+
this.buildAgent.warn(e.cause?.message ?? 'An unknown error occurred while fetching data')
247+
return Response.error()
248+
})
246249
if (!nugetIndex?.ok) {
247250
this.buildAgent.warn(`Failed to fetch data from NuGet source ${nugetSource}.`)
248251
continue
@@ -262,21 +265,26 @@ export abstract class DotnetTool implements IDotnetTool {
262265
}
263266

264267
private async queryVersionsFromNugetSource(serviceUrl: string, toolName: string, includePrerelease: boolean): Promise<string[]> {
268+
this.buildAgent.debug(`Fetching ${toolName} versions from source ${serviceUrl}`)
265269
const toolNameParam = encodeURIComponent(toolName.toLowerCase())
266270
const prereleaseParam = includePrerelease ? 'true' : 'false'
267271
const downloadPath = `${serviceUrl}?q=${toolNameParam}&prerelease=${prereleaseParam}&semVerLevel=2.0.0&take=1`
268272

269-
const response = await fetch(downloadPath)
273+
const response = await fetch(downloadPath).catch((e: { cause: { message: string | undefined } | undefined }) => {
274+
this.buildAgent.warn(e.cause?.message ?? 'An unknown error occurred while fetching data')
275+
return Response.error()
276+
})
270277

271278
if (!response || !response.ok) {
272279
this.buildAgent.warn(`failed to query latest version for ${toolName} from ${downloadPath}. Status code: ${response ? response.status : 'unknown'}`)
273280
return []
274281
}
275282
const { data } = (await response.json()) as NugetVersions
276283

277-
const versions = data[0].versions.map(x => x.version)
284+
const versions = data?.[0]?.versions?.map(x => x.version) ?? []
278285

279-
return versions ?? []
286+
this.buildAgent.debug(`Found ${versions.length} versions: ${versions.join(', ')}`)
287+
return versions
280288
}
281289

282290
private async queryLatestMatch(toolName: string, versionSpec: string, includePrerelease: boolean): Promise<string | null> {

0 commit comments

Comments
 (0)