Convert to graceful exception (#8751)

This commit is contained in:
William Lee 2018-03-08 15:49:16 -08:00 committed by GitHub
parent 4c2b07023a
commit 3861fc1700
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 114 deletions

View file

@ -2,16 +2,35 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.DotNet.Cli.Utils
{
public class GracefulException : Exception
{
public bool IsUserError { get; } = true;
public string VerboseMessage { get; } = string.Empty;
public GracefulException(string message) : base(message)
{
Data.Add(ExceptionExtensions.CLI_User_Displayed_Exception, true);
}
public GracefulException(IEnumerable<string> messages, IEnumerable<string> verboseMessages = null,
bool isUserError = true)
: base(string.Join(Environment.NewLine, messages))
{
IsUserError = isUserError;
if (verboseMessages != null)
{
VerboseMessage = string.Join(Environment.NewLine, VerboseMessage);
}
Data.Add(ExceptionExtensions.CLI_User_Displayed_Exception, true);
}
public GracefulException(string format, params string[] args) : this(string.Format(format, args))
{
}

View file

@ -42,19 +42,26 @@ namespace Microsoft.DotNet.Cli
}
catch (KeyNotFoundException)
{
return ReportError(CommonLocalizableStrings.RequiredCommandNotPassed);
Reporter.Error.WriteLine(CommonLocalizableStrings.RequiredCommandNotPassed.Red());
ParseResult.ShowHelp();
return 1;
}
catch (GracefulException e)
{
return ReportError(e.Message);
if (Reporter.IsVerbose)
{
Reporter.Error.WriteLine(e.VerboseMessage.Red());
}
Reporter.Error.WriteLine(e.Message.Red());
if (e.IsUserError)
{
ParseResult.ShowHelp();
}
return 1;
}
}
private int ReportError(string errorMessage)
{
Reporter.Error.WriteLine(errorMessage.Red());
ParseResult.ShowHelp();
return 1;
}
}
}
}

View file

@ -163,43 +163,41 @@ namespace Microsoft.DotNet.Tools.Install.Tool
}
catch (ToolPackageException ex)
{
if (Reporter.IsVerbose)
{
Reporter.Verbose.WriteLine(ex.ToString().Red());
}
_errorReporter.WriteLine(ex.Message.Red());
_errorReporter.WriteLine(string.Format(LocalizableStrings.ToolInstallationFailed, _packageId).Red());
return 1;
throw new GracefulException(
messages: new[]
{
ex.Message,
string.Format(LocalizableStrings.ToolInstallationFailed, _packageId),
},
verboseMessages: new[] {ex.ToString()},
isUserError: false);
}
catch (ToolConfigurationException ex)
{
if (Reporter.IsVerbose)
{
Reporter.Verbose.WriteLine(ex.ToString().Red());
}
_errorReporter.WriteLine(
string.Format(
LocalizableStrings.InvalidToolConfiguration,
ex.Message).Red());
_errorReporter.WriteLine(string.Format(LocalizableStrings.ToolInstallationFailedContactAuthor, _packageId).Red());
return 1;
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.InvalidToolConfiguration,
ex.Message),
string.Format(LocalizableStrings.ToolInstallationFailedContactAuthor, _packageId)
},
verboseMessages: new[] {ex.ToString()},
isUserError: false);
}
catch (ShellShimException ex)
{
if (Reporter.IsVerbose)
{
Reporter.Verbose.WriteLine(ex.ToString().Red());
}
_errorReporter.WriteLine(
string.Format(
LocalizableStrings.FailedToCreateToolShim,
_packageId,
ex.Message).Red());
_errorReporter.WriteLine(string.Format(LocalizableStrings.ToolInstallationFailed, _packageId).Red());
return 1;
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.FailedToCreateToolShim,
_packageId,
ex.Message),
string.Format(LocalizableStrings.ToolInstallationFailed, _packageId)
},
verboseMessages: new[] {ex.ToString()},
isUserError: false);
}
}
}

View file

@ -74,20 +74,26 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tool
package = toolPackageStore.EnumeratePackageVersions(packageId).SingleOrDefault();
if (package == null)
{
_errorReporter.WriteLine(
string.Format(
LocalizableStrings.ToolNotInstalled,
packageId).Red());
return 1;
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.ToolNotInstalled,
packageId),
},
isUserError: false);
}
}
catch (InvalidOperationException)
{
_errorReporter.WriteLine(
string.Format(
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.ToolHasMultipleVersionsInstalled,
packageId).Red());
return 1;
packageId),
},
isUserError: false);
}
try
@ -115,27 +121,26 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tool
}
catch (ToolPackageException ex)
{
if (Reporter.IsVerbose)
{
Reporter.Verbose.WriteLine(ex.ToString().Red());
}
_errorReporter.WriteLine(ex.Message.Red());
return 1;
throw new GracefulException(
messages: new[]
{
ex.Message
},
verboseMessages: new[] { ex.ToString() },
isUserError: false);
}
catch (Exception ex) when (ex is ToolConfigurationException || ex is ShellShimException)
{
if (Reporter.IsVerbose)
{
Reporter.Verbose.WriteLine(ex.ToString().Red());
}
_errorReporter.WriteLine(
string.Format(
throw new GracefulException(
messages: new[]
{
string.Format(
LocalizableStrings.FailedToUninstallTool,
packageId,
ex.Message).Red());
return 1;
ex.Message)
},
verboseMessages: new[] { ex.ToString() },
isUserError: false);
}
}
}

View file

@ -152,14 +152,10 @@ namespace Microsoft.DotNet.Tests.Commands
_environmentPathInstructionMock,
_reporter);
installToolCommand.Execute().Should().Be(1);
Action a = () => installToolCommand.Execute();
_reporter
.Lines
.Should()
.Equal(
"Simulated error".Red(),
string.Format(LocalizableStrings.ToolInstallationFailed, PackageId).Red());
a.ShouldThrow<GracefulException>().And.Message
.Should().Contain(string.Format(LocalizableStrings.ToolInstallationFailed, PackageId));
_fileSystem.Directory.Exists(Path.Combine(PathToPlacePackages, PackageId)).Should().BeFalse();
}
@ -177,15 +173,12 @@ namespace Microsoft.DotNet.Tests.Commands
_environmentPathInstructionMock,
_reporter);
installToolCommand.Execute().Should().Be(1);
Action a = () => installToolCommand.Execute();
_reporter
.Lines[0]
.Should()
.Contain(
string.Format(
CommonLocalizableStrings.ShellShimConflict,
ProjectRestorerMock.FakeCommandName));
a.ShouldThrow<GracefulException>().And.Message
.Should().Contain(string.Format(
CommonLocalizableStrings.ShellShimConflict,
ProjectRestorerMock.FakeCommandName));
_fileSystem.Directory.Exists(Path.Combine(PathToPlacePackages, PackageId)).Should().BeFalse();
}
@ -205,16 +198,15 @@ namespace Microsoft.DotNet.Tests.Commands
_environmentPathInstructionMock,
_reporter);
installToolCommand.Execute().Should().Be(1);
Action a = () => installToolCommand.Execute();
_reporter
.Lines
.Should()
.Equal(
a.ShouldThrow<GracefulException>().And.Message
.Should().Contain(
string.Format(
LocalizableStrings.InvalidToolConfiguration,
"Simulated error").Red(),
string.Format(LocalizableStrings.ToolInstallationFailedContactAuthor, PackageId).Red());
"Simulated error") + Environment.NewLine +
string.Format(LocalizableStrings.ToolInstallationFailedContactAuthor, PackageId)
);
}
[Fact]
@ -330,15 +322,14 @@ namespace Microsoft.DotNet.Tests.Commands
new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true),
_reporter);
installToolCommand.Execute().Should().Be(1);
Action a = () => installToolCommand.Execute();
_reporter
.Lines
.Should()
.Equal(
$"Error: failed to restore package {PackageId}.", // From mock implementation, not localized
LocalizableStrings.ToolInstallationRestoreFailed.Red(),
string.Format(LocalizableStrings.ToolInstallationFailed, PackageId).Red());
a.ShouldThrow<GracefulException>().And.Message
.Should().Contain(
LocalizableStrings.ToolInstallationRestoreFailed +
Environment.NewLine + string.Format(LocalizableStrings.ToolInstallationFailed, PackageId));
_fileSystem.Directory.Exists(Path.Combine(PathToPlacePackages, PackageId)).Should().BeFalse();
}
[Fact]

View file

@ -49,15 +49,13 @@ namespace Microsoft.DotNet.Tests.Commands
var packageId = "does.not.exist";
var command = CreateUninstallCommand($"-g {packageId}");
command.Execute().Should().Be(1);
_reporter.Lines.Count.Should().Be(1);
Action a = () => command.Execute();
_reporter
.Lines[0]
.Should()
.Be(string.Format(
a.ShouldThrow<GracefulException>().And.Message
.Should().Contain(
string.Format(
LocalizableStrings.ToolNotInstalled,
packageId).Red());
packageId));
}
[Fact]
@ -125,20 +123,15 @@ namespace Microsoft.DotNet.Tests.Commands
_fileSystem.Directory.Exists(packageDirectory.Value).Should().BeTrue();
_fileSystem.File.Exists(shimPath).Should().BeTrue();
_reporter.Lines.Clear();
CreateUninstallCommand(
Action a = () => CreateUninstallCommand(
options: $"-g {PackageId}",
uninstallCallback: () => throw new IOException("simulated error"))
.Execute()
.Should()
.Be(1);
.Execute();
_reporter
.Lines
.Single()
.Should()
.Contain(string.Format(
a.ShouldThrow<GracefulException>()
.And.Message
.Should().Contain(
string.Format(
CommonLocalizableStrings.FailedToUninstallToolPackage,
PackageId,
"simulated error"));