Skip to content

Commit dc13650

Browse files
committed
C#: Simplify and improve the reachability check and improve the logging.
1 parent 650e382 commit dc13650

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,9 @@ private void TryChangeProjectFile(DirectoryInfo projectDir, Regex pattern, strin
666666
}
667667
}
668668

669-
private static async Task ExecuteGetRequest(string address, HttpClient httpClient, CancellationToken cancellationToken)
669+
private static async Task<HttpResponseMessage> ExecuteGetRequest(string address, HttpClient httpClient, CancellationToken cancellationToken)
670670
{
671-
using var stream = await httpClient.GetStreamAsync(address, cancellationToken);
672-
var buffer = new byte[1024];
673-
int bytesRead;
674-
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
675-
{
676-
// do nothing
677-
}
671+
return await httpClient.GetAsync(address, cancellationToken);
678672
}
679673

680674
private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, bool allowNonTimeoutExceptions)
@@ -716,7 +710,9 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
716710
cts.CancelAfter(timeoutMilliSeconds);
717711
try
718712
{
719-
ExecuteGetRequest(feed, client, cts.Token).GetAwaiter().GetResult();
713+
logger.LogInfo($"Attempt {i + 1}/{tryCount} to reach NuGet feed '{feed}'.");
714+
var response = ExecuteGetRequest(feed, client, cts.Token).GetAwaiter().GetResult();
715+
response.EnsureSuccessStatusCode();
720716
logger.LogInfo($"Querying NuGet feed '{feed}' succeeded.");
721717
return true;
722718
}
@@ -731,9 +727,11 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
731727
continue;
732728
}
733729

734-
// We're only interested in timeouts.
735-
var start = allowNonTimeoutExceptions ? "Considering" : "Not considering";
736-
logger.LogInfo($"Querying NuGet feed '{feed}' failed in a timely manner. {start} the feed for use. The reason for the failure: {exc.Message}");
730+
// Adjust the message based on whether non-timeout exceptions are allowed.
731+
var useMessage = allowNonTimeoutExceptions
732+
? "Considering the feed for use despite of the failure as it wasn't a timeout."
733+
: "Not considering the feed for use.";
734+
logger.LogInfo($"Querying NuGet feed '{feed}' failed. {useMessage} The reason for the failure: {exc.Message}");
737735
return allowNonTimeoutExceptions;
738736
}
739737
}

0 commit comments

Comments
 (0)