Skip to content

Commit 6a28e8a

Browse files
committed
Clean up error experience when downloading non-tools
1 parent 211ef6f commit 6a28e8a

19 files changed

+90
-8
lines changed

src/Cli/dotnet/NugetPackageDownloader/INuGetPackageDownloader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Task<string> DownloadPackageAsync(PackageId packageId,
1616
bool includePreview = false,
1717
bool? includeUnlisted = null,
1818
DirectoryPath? downloadFolder = null,
19-
PackageSourceMapping packageSourceMapping = null);
19+
PackageSourceMapping packageSourceMapping = null,
20+
bool isTool = false);
2021

2122
Task<string> GetPackageUrl(PackageId packageId,
2223
NuGetVersion packageVersion = null,

src/Cli/dotnet/NugetPackageDownloader/LocalizableStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
<data name="IsNotFoundInNuGetFeeds" xml:space="preserve">
127127
<value>{0} is not found in NuGet feeds {1}.</value>
128128
</data>
129+
<data name="NotATool" xml:space="preserve">
130+
<value>Package {0} is not a dotnet tool.</value>
131+
</data>
129132
<data name="DownloadVersionFailed" xml:space="preserve">
130133
<value>Downloading {0} version {1} failed.</value>
131134
</data>

src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,30 @@ public async Task<string> DownloadPackageAsync(PackageId packageId,
8686
bool includePreview = false,
8787
bool? includeUnlisted = null,
8888
DirectoryPath? downloadFolder = null,
89-
PackageSourceMapping packageSourceMapping = null)
89+
PackageSourceMapping packageSourceMapping = null,
90+
bool isTool = false)
9091
{
9192
CancellationToken cancellationToken = CancellationToken.None;
9293

9394
(var source, var resolvedPackageVersion) = await GetPackageSourceAndVersion(packageId, packageVersion,
9495
packageSourceLocation, includePreview, includeUnlisted ?? packageVersion is not null, packageSourceMapping).ConfigureAwait(false);
9596

96-
FindPackageByIdResource resource = null;
9797
SourceRepository repository = GetSourceRepository(source);
9898

99-
resource = await repository.GetResourceAsync<FindPackageByIdResource>(cancellationToken)
100-
.ConfigureAwait(false);
99+
if (isTool && await repository.GetResourceAsync<ServiceIndexResourceV3>().ConfigureAwait(false) is ServiceIndexResourceV3 serviceIndex)
100+
{
101+
var uri = serviceIndex.GetServiceEntries("SearchQueryService/3.5.0")[0].Uri;
102+
var queryUri = uri + $"?q={packageId}&packageType=dotnettool";
103+
using HttpClient client = new(new HttpClientHandler() { CheckCertificateRevocationList = true });
104+
using HttpResponseMessage response = await client.GetAsync(queryUri).ConfigureAwait(false);
105+
if (response.Content.Headers.ContentLength == 139)
106+
{
107+
throw new ToolPackageException(string.Format(LocalizableStrings.NotATool, packageId));
108+
}
109+
}
101110

111+
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>(cancellationToken)
112+
.ConfigureAwait(false);
102113
if (resource == null)
103114
{
104115
throw new NuGetPackageNotFoundException(

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.ko.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)