Extract error messages to separate class

This commit is contained in:
Krzysztof Wicher 2016-11-23 10:41:30 -08:00
parent 3fc0517cda
commit 592af9dd61
4 changed files with 46 additions and 16 deletions

View file

@ -12,6 +12,10 @@ namespace Microsoft.DotNet.Cli.Utils
{
}
public GracefulException(string format, params string[] args) : this(string.Format(format, args))
{
}
public GracefulException(string message, Exception innerException) : base(message, innerException)
{
}

27
src/dotnet/Strings.cs Normal file
View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.DotNet.Tools
{
internal static class Strings
{
// Arguments parsing
public const string RequiredArgumentNotPassed = "Required argument {0} was not passed.";
public const string RequiredArgumentIsInvalid = "Required argument {0} is invalid.";
// Project
public const string CouldNotFindProjectOrDirectory = "Could not find project or directory `{0}`.";
public const string CouldNotFindAnyProjectInDirectory = "Could not find any project in `{0}`.";
public const string MoreThanOneProjectInDirectory = "Found more than one project in `{0}`. Please specify which one to use.";
public const string FoundInvalidProject = "Found a project `{0}` but it is invalid.";
public const string ProjectIsInvalid = "Invalid project `{0}`.";
public const string ProjectDoesNotExist = "Project `{0}` does not exist.";
// Project Reference
public const string ProjectAlreadyHasAreference = "Project already has a reference to `{0}`.";
public const string ReferenceAddedToTheProject = "Reference `{0}` added to the project.";
public const string ReferenceDoesNotExist = "Reference `{0}` does not exist.";
public const string SpecifyAtLeastOneReference = "You must specify at least one reference to add.";
}
}

View file

@ -49,7 +49,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
app.OnExecute(() => {
if (string.IsNullOrEmpty(projectArgument.Value))
{
throw new GracefulException("Argument <Project> is required.");
throw new GracefulException(Strings.RequiredArgumentNotPassed, "<Project>");
}
ProjectRootElement project;
@ -69,7 +69,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
if (app.RemainingArguments.Count == 0)
{
throw new GracefulException("You must specify at least one reference to add.");
throw new GracefulException(Strings.SpecifyAtLeastOneReference);
}
List<string> references = app.RemainingArguments;
@ -120,7 +120,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
throw new GracefulException(
string.Join(
Environment.NewLine,
notExisting.Select((ne) => $"Reference `{ne}` does not exist.")));
notExisting.Select((r) => string.Format(Strings.ReferenceDoesNotExist, r))));
}
}
@ -148,19 +148,18 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
{
if (!File.Exists(filename))
{
throw new GracefulException($"Provided project `{filename}` does not exist.");
throw new GracefulException(Strings.ProjectDoesNotExist, filename);
}
var project = TryOpenProject(filename);
if (project == null)
{
throw new GracefulException($"Invalid project `{filename}`.");
throw new GracefulException(Strings.ProjectIsInvalid, filename);
}
return project;
}
// TODO: improve errors
internal static ProjectRootElement GetProjectFromDirectoryOrThrow(string directory)
{
DirectoryInfo dir;
@ -170,36 +169,36 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
}
catch (ArgumentException)
{
throw new GracefulException($"Could not find project or directory `{directory}`.");
throw new GracefulException(Strings.CouldNotFindProjectOrDirectory, directory);
}
if (!dir.Exists)
{
throw new GracefulException($"Could not find project or directory `{directory}`.");
throw new GracefulException(Strings.CouldNotFindProjectOrDirectory, directory);
}
FileInfo[] files = dir.GetFiles("*proj");
if (files.Length == 0)
{
throw new GracefulException($"Could not find any project in `{directory}`.");
throw new GracefulException(Strings.CouldNotFindAnyProjectInDirectory, directory);
}
if (files.Length > 1)
{
throw new GracefulException("Found more than one project in the current directory. Please specify which one to use.");
throw new GracefulException(Strings.MoreThanOneProjectInDirectory, directory);
}
FileInfo projectFile = files.First();
if (!projectFile.Exists)
{
throw new GracefulException($"Could not find any project in `{directory}`.");
throw new GracefulException(Strings.CouldNotFindAnyProjectInDirectory, directory);
}
var ret = TryOpenProject(projectFile.FullName);
if (ret == null)
{
throw new GracefulException($"Found a project `{projectFile.FullName}` but it is invalid.");
throw new GracefulException(Strings.FoundInvalidProject, projectFile.FullName);
}
return ret;
@ -220,14 +219,14 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
{
if (root.HasExistingItemWithCondition(framework, @ref))
{
Reporter.Output.WriteLine($"Project already has a reference to `{@ref}`.");
Reporter.Output.WriteLine(string.Format(Strings.ProjectAlreadyHasAreference, @ref));
continue;
}
numberOfAddedReferences++;
itemGroup.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref));
Reporter.Output.WriteLine($"Reference `{@ref}` added to the project.");
Reporter.Output.WriteLine(string.Format(Strings.ReferenceAddedToTheProject, @ref));
}
return numberOfAddedReferences;

View file

@ -58,7 +58,7 @@ Commands:
}
else if (args.Length == 1)
{
Reporter.Error.WriteLine("Required argument <command> was not passed.".Red());
Reporter.Error.WriteLine(string.Format(Strings.RequiredArgumentNotPassed, "<command>").Red());
Reporter.Output.WriteLine(HelpText);
return 1;
}
@ -76,7 +76,7 @@ Commands:
return builtin(args);
}
Reporter.Error.WriteLine("Required argument <command> is invalid.".Red());
Reporter.Error.WriteLine(string.Format(Strings.RequiredArgumentIsInvalid, "<command>").Red());
Reporter.Output.WriteLine(HelpText);
return 1;
}