From 0b746ee559af60731ff49354fddc12c857ccfac4 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 12 Apr 2016 16:44:34 -0700 Subject: [PATCH] improve error message for unresolved tools --- .../CommandResolution/ToolPathCalculator.cs | 25 +++++++++++++++++++ .../CommandUnknownException.cs | 2 +- .../GracefulEceptionException.cs | 19 ++++++++++++++ src/dotnet/Program.cs | 6 ++--- 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/Microsoft.DotNet.Cli.Utils/GracefulEceptionException.cs diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ToolPathCalculator.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ToolPathCalculator.cs index 5dd24c0c5..b89fa6fc7 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ToolPathCalculator.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ToolPathCalculator.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Collections.Generic; using NuGet.Frameworks; @@ -19,15 +20,39 @@ namespace Microsoft.DotNet.Cli.Utils public string GetBestLockFilePath(string packageId, VersionRange versionRange, NuGetFramework framework) { + if (versionRange == null) + { + throw new ArgumentNullException(nameof(versionRange)); + } + + if (framework == null) + { + throw new ArgumentNullException(nameof(framework)); + } + var availableToolVersions = GetAvailableToolVersions(packageId); var bestVersion = versionRange.FindBestMatch(availableToolVersions); + if (bestVersion == null) + { + throw new GracefulException($"Version for package `{packageId}` could not be resolved."); + } return GetLockFilePath(packageId, bestVersion, framework); } public string GetLockFilePath(string packageId, NuGetVersion version, NuGetFramework framework) { + if (version == null) + { + throw new ArgumentNullException(nameof(version)); + } + + if (framework == null) + { + throw new ArgumentNullException(nameof(framework)); + } + return Path.Combine( GetBaseToolPath(packageId), version.ToNormalizedString(), diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandUnknownException.cs b/src/Microsoft.DotNet.Cli.Utils/CommandUnknownException.cs index 34abe1e2b..d3d6fa988 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandUnknownException.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandUnknownException.cs @@ -2,7 +2,7 @@ namespace Microsoft.DotNet.Cli.Utils { - public class CommandUnknownException : Exception + public class CommandUnknownException : GracefulException { public CommandUnknownException() { diff --git a/src/Microsoft.DotNet.Cli.Utils/GracefulEceptionException.cs b/src/Microsoft.DotNet.Cli.Utils/GracefulEceptionException.cs new file mode 100644 index 000000000..4c061e4b7 --- /dev/null +++ b/src/Microsoft.DotNet.Cli.Utils/GracefulEceptionException.cs @@ -0,0 +1,19 @@ +using System; + +namespace Microsoft.DotNet.Cli.Utils +{ + public class GracefulException : Exception + { + public GracefulException() + { + } + + public GracefulException(string message) : base(message) + { + } + + public GracefulException(string message, Exception innerException) : base(message, innerException) + { + } + } +} \ No newline at end of file diff --git a/src/dotnet/Program.cs b/src/dotnet/Program.cs index 8833881c7..9d212a2f1 100644 --- a/src/dotnet/Program.cs +++ b/src/dotnet/Program.cs @@ -34,13 +34,11 @@ namespace Microsoft.DotNet.Cli { return Program.ProcessArgs(args, new Telemetry()); } - catch (CommandUnknownException e) + catch (GracefulException e) { - Console.WriteLine(e.Message); - + Console.WriteLine(e.Message.Red().Bold()); return 1; } - } internal static int ProcessArgs(string[] args, ITelemetry telemetryClient)