Skip to content

Commit

Permalink
Change the format of tool exceptions and NuGet API exceptions to be g…
Browse files Browse the repository at this point in the history
…raceful (#36641)
  • Loading branch information
JL03-Yue authored Nov 8, 2023
2 parents 48c338f + 34bd038 commit f484f4c
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/GracefulException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class GracefulException : Exception
public bool IsUserError { get; } = true;
public string VerboseMessage { get; } = string.Empty;

public GracefulException()
{
}

public GracefulException(string message) : base(message)
{
Data.Add(ExceptionExtensions.CLI_User_Displayed_Exception, true);
Expand Down
15 changes: 15 additions & 0 deletions src/Cli/dotnet/NugetPackageDownloader/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@
<data name="FailedToMapSourceUnderPackageSourceMapping" xml:space="preserve">
<value>Package Source Mapping is enabled, but no source mapped under the specified package ID: {0}. See the documentation for Package Source Mapping at https://aka.ms/nuget-package-source-mapping for more details.</value>
</data>
<data name="PackageVersionDescriptionDefault" xml:space="preserve">
<value>A version of {0} of package {1}</value>
</data>
<data name="PackageVersionDescriptionForExactVersionMatch" xml:space="preserve">
<value>Version {0} of package {1}</value>
</data>
<data name="PackageVersionDescriptionForVersionWithLowerAndUpperBounds" xml:space="preserve">
<value>A version between {0} and {1} of package {2}</value>
</data>
<data name="PackageVersionDescriptionForVersionWithLowerBound" xml:space="preserve">
<value>A version higher than {0} of package {1}</value>
</data>
<data name="PackageVersionDescriptionForVersionWithUpperBound" xml:space="preserve">
<value>A version less than {0} of package {1}</value>
</data>
</root>
49 changes: 41 additions & 8 deletions src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools;
using Microsoft.Extensions.EnvironmentAbstractions;
using Microsoft.TemplateEngine.Abstractions;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Credentials;
Expand Down Expand Up @@ -162,7 +164,7 @@ public async Task<string> GetPackageUrl(PackageId packageId,
bool includePreview = false)
{
(var source, var resolvedPackageVersion) = await GetPackageSourceAndVersion(packageId, packageVersion, packageSourceLocation, includePreview).ConfigureAwait(false);

SourceRepository repository = GetSourceRepository(source);
if (repository.PackageSource.IsLocal)
{
Expand Down Expand Up @@ -450,15 +452,45 @@ await Task.WhenAll(
throw new NuGetPackageNotFoundException(
string.Format(
LocalizableStrings.IsNotFoundInNuGetFeeds,
$"{packageIdentifier}::{versionRange}",
GenerateVersionRangeErrorDescription(packageIdentifier, versionRange),
string.Join(", ", packageSources.Select(source => source.Source))));
}
}

private string GenerateVersionRangeErrorDescription(string packageIdentifier, VersionRange versionRange)
{
if (!string.IsNullOrEmpty(versionRange.OriginalString) && versionRange.OriginalString == "*")
{
return $"{packageIdentifier}";
}
else if (versionRange.HasLowerAndUpperBounds && versionRange.MinVersion == versionRange.MaxVersion)
{
return string.Format(LocalizableStrings.PackageVersionDescriptionForExactVersionMatch,
versionRange.MinVersion, packageIdentifier);
}
else if (versionRange.HasLowerAndUpperBounds)
{
return string.Format(LocalizableStrings.PackageVersionDescriptionForVersionWithLowerAndUpperBounds,
versionRange.MinVersion, versionRange.MaxVersion, packageIdentifier);
}
else if (versionRange.HasLowerBound)
{
return string.Format(LocalizableStrings.PackageVersionDescriptionForVersionWithLowerBound,
versionRange.MinVersion, packageIdentifier);
}
else if (versionRange.HasUpperBound)
{
return string.Format(LocalizableStrings.PackageVersionDescriptionForVersionWithUpperBound,
versionRange.MaxVersion, packageIdentifier);
}

// Default message if the format doesn't match any of the expected cases
return string.Format(LocalizableStrings.PackageVersionDescriptionDefault, versionRange, packageIdentifier);
}

private async Task<(PackageSource, IPackageSearchMetadata)> GetLatestVersionInternalAsync(
string packageIdentifier, IEnumerable<PackageSource> packageSources, bool includePreview,
CancellationToken cancellationToken)
private async Task<(PackageSource, IPackageSearchMetadata)> GetLatestVersionInternalAsync(
string packageIdentifier, IEnumerable<PackageSource> packageSources, bool includePreview,
CancellationToken cancellationToken)
{
if (packageSources == null)
{
Expand Down Expand Up @@ -499,7 +531,7 @@ await Task.WhenAll(
.SelectMany(result => result.foundPackages.Select(package => (result.source, package)));

if (!accumulativeSearchResults.Any())
{
{
throw new NuGetPackageNotFoundException(
string.Format(
LocalizableStrings.IsNotFoundInNuGetFeeds,
Expand Down Expand Up @@ -527,7 +559,7 @@ public async Task<NuGetVersion> GetBestPackageVersionAsync(PackageId packageId,
VersionRange versionRange,
PackageSourceLocation packageSourceLocation = null)
{
if(versionRange.MinVersion != null && versionRange.MaxVersion != null && versionRange.MinVersion == versionRange.MaxVersion)
if (versionRange.MinVersion != null && versionRange.MaxVersion != null && versionRange.MinVersion == versionRange.MaxVersion)
{
return versionRange.MinVersion;
}
Expand Down Expand Up @@ -624,7 +656,8 @@ bool TryGetPackageMetadata(
}

throw new NuGetPackageNotFoundException(string.Format(LocalizableStrings.IsNotFoundInNuGetFeeds,
$"{packageIdentifier}::{packageVersion}", string.Join(";", sources.Select(s => s.Source))));
GenerateVersionRangeErrorDescription(packageIdentifier, new VersionRange(minVersion: packageVersion, maxVersion: packageVersion, includeMaxVersion: true)),
string.Join(";", sources.Select(s => s.Source))));
}

private async Task<(PackageSource source, IEnumerable<IPackageSearchMetadata> foundPackages)>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli.NuGetPackageDownloader
{
internal class NuGetPackageInstallerException : Exception
internal class NuGetPackageInstallerException : GracefulException
{
public NuGetPackageInstallerException()
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f484f4c

Please sign in to comment.