Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message from FindDotNetCliPackage task #6630

Merged
2 commits merged into from
Dec 2, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 30 additions & 35 deletions src/Microsoft.DotNet.Helix/Sdk/FindDotNetCliPackage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -51,38 +48,43 @@ private async Task ExecuteAsync()
NormalizeParameters();
await ResolveVersionAsync();

var downloadUrl = GetDownloadUrl();
string downloadUrl = GetDownloadUrl();

Log.LogMessage($"Retrieved dotnet cli {PackageType} version {Version} package uri {downloadUrl}, testing...");

using (var req = new HttpRequestMessage(HttpMethod.Head, downloadUrl))
try
{
var res = await _client.SendAsync(req);
using var req = new HttpRequestMessage(HttpMethod.Head, downloadUrl);
using HttpResponseMessage res = await _client.SendAsync(req);
res.EnsureSuccessStatusCode();
}

Log.LogMessage($"Url {downloadUrl} is valid.");

PackageUri = downloadUrl;
}

private string GetDownloadUrl()
{
var extension = Runtime.StartsWith("win") ? "zip" : "tar.gz";
if (PackageType == "sdk")
catch (HttpRequestException ex) when (ex.Message.Contains("404 (Not Found)")) // 404 means that we successfully hit the server, and it returned 404. This cannot be a network hick-up
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
catch (HttpRequestException ex) when (ex.Message.Contains("404 (Not Found)")) // 404 means that we successfully hit the server, and it returned 404. This cannot be a network hick-up
catch (HttpRequestException ex) when (ex.Message.Contains("404 (Not Found)")) // 404 means that we successfully hit the server, and it returned 404. This cannot be a network hiccup

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would REALLY rather we check the result code on line 59, rather than parse the error message. Usually it's a pain to do that, so the catch is ok... but it's just 2 lines higher, and avoid the message parsing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: hiccup.

{
return $"{DotNetCliAzureFeed}/Sdk/{Version}/dotnet-sdk-{Version}-{Runtime}.{extension}";
Log.LogError(FailureCategory.Build, $"Unable to find dotnet cli {PackageType} version {Version}, tried {downloadUrl}");
}
else if (PackageType == "aspnetcore-runtime")
catch (HttpRequestException otherEx)
{
return $"{DotNetCliAzureFeed}/aspnetcore/Runtime/{Version}/aspnetcore-runtime-{Version}-{Runtime}.{extension}";
Log.LogError(FailureCategory.Build, $"Unable to access dotnet cli {PackageType} version {Version} at {downloadUrl}, {otherEx.Message}");
}
else // PackageType == "runtime"

if (!Log.HasLoggedErrors)
{
return $"{DotNetCliAzureFeed}/Runtime/{Version}/dotnet-runtime-{Version}-{Runtime}.{extension}";
Log.LogMessage($"Url {downloadUrl} is valid.");
PackageUri = downloadUrl;
}
}

private string GetDownloadUrl()
{
string extension = Runtime.StartsWith("win") ? "zip" : "tar.gz";
return PackageType switch
{
"sdk" => $"{DotNetCliAzureFeed}/Sdk/{Version}/dotnet-sdk-{Version}-{Runtime}.{extension}",
"aspnetcore-runtime" => $"{DotNetCliAzureFeed}/aspnetcore/Runtime/{Version}/aspnetcore-runtime-{Version}-{Runtime}.{extension}",
_ => $"{DotNetCliAzureFeed}/Runtime/{Version}/dotnet-runtime-{Version}-{Runtime}.{extension}"
};
}

private void NormalizeParameters()
{
if (string.Equals(Channel, "lts", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -126,23 +128,16 @@ private async Task ResolveVersionAsync()
if (Version == "latest")
{
Log.LogMessage(MessageImportance.Low, "Resolving latest dotnet cli version.");
string latestVersionUrl;
if (PackageType == "sdk")
{
latestVersionUrl = $"{DotNetCliAzureFeed}/Sdk/{Channel}/latest.version";
}
else if (PackageType == "aspnetcore-runtime")
{
latestVersionUrl = $"{DotNetCliAzureFeed}/aspnetcore/Runtime/{Channel}/latest.version";
}
else // PackageType == "runtime"
string latestVersionUrl = PackageType switch
{
latestVersionUrl = $"{DotNetCliAzureFeed}/Runtime/{Channel}/latest.version";
}
"sdk" => $"{DotNetCliAzureFeed}/Sdk/{Channel}/latest.version",
"aspnetcore-runtime" => $"{DotNetCliAzureFeed}/aspnetcore/Runtime/{Channel}/latest.version",
_ => $"{DotNetCliAzureFeed}/Runtime/{Channel}/latest.version"
};

Log.LogMessage(MessageImportance.Low, $"Resolving latest version from url {latestVersionUrl}");
var latestVersionContent = await _client.GetStringAsync(latestVersionUrl);
var versionData = latestVersionContent.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
string latestVersionContent = await _client.GetStringAsync(latestVersionUrl);
string[] versionData = latestVersionContent.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
Version = versionData[1];
Log.LogMessage(MessageImportance.Low, $"Got latest dotnet cli version {Version}");
}
Expand Down