Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SolutionArgumentException with error types #74

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ private ProjectTypeTable(bool isBuiltIn, List<ProjectType> projectTypes)
!this.fromExtension.TryAdd(GetExtension(type.Extension), type))
{
string projectType = type.GetDisplayName();
throw new SolutionException(string.Format(Errors.DuplicateExtension_Args2, type.Extension, projectType));
throw new SolutionException(string.Format(Errors.DuplicateExtension_Args2, type.Extension, projectType), SolutionErrorType.DuplicateExtension);
}

if (!type.Name.IsNullOrEmpty())
{
if (!this.fromName.TryAdd(type.Name, type))
{
string projectType = type.GetDisplayName();
throw new SolutionException(string.Format(Errors.DuplicateName_Args2, type.Name, projectType));
throw new SolutionException(string.Format(Errors.DuplicateName_Args2, type.Name, projectType), SolutionErrorType.DuplicateName);
}

// If a name isn't provided, it is just to map an extension to a project type.
if (type.ProjectTypeId != Guid.Empty && !this.fromProjectTypeId.TryAdd(type.ProjectTypeId, type))
{
string projectType = type.GetDisplayName();
throw new SolutionException(string.Format(Errors.DuplicateProjectTypeId_Args2, type.ProjectTypeId, projectType));
throw new SolutionException(string.Format(Errors.DuplicateProjectTypeId_Args2, type.ProjectTypeId, projectType), SolutionErrorType.DuplicateProjectTypeId);
}
}

if (string.IsNullOrEmpty(type.Name) && string.IsNullOrEmpty(type.Extension) && type.ProjectTypeId == Guid.Empty)
{
if (this.defaultRules is not null)
{
throw new SolutionException(Errors.DuplicateDefaultProjectType);
throw new SolutionException(Errors.DuplicateDefaultProjectType, SolutionErrorType.DuplicateDefaultProjectType);
}

this.defaultRules ??= type.ConfigurationRules;
Expand All @@ -77,7 +77,7 @@ private ProjectTypeTable(bool isBuiltIn, List<ProjectType> projectTypes)
{
if (this.GetBasedOnType(type) is null)
{
throw new SolutionException(string.Format(Errors.InvalidProjectTypeReference_Args1, type.BasedOn));
throw new SolutionException(string.Format(Errors.InvalidProjectTypeReference_Args1, type.BasedOn), SolutionErrorType.InvalidProjectTypeReference);
}

// Check for loops in the BasedOn chain using Floyd's cycle-finding algorithm.
Expand All @@ -88,7 +88,7 @@ private ProjectTypeTable(bool isBuiltIn, List<ProjectType> projectTypes)
if (object.ReferenceEquals(currentSlow, currentFast))
{
string projectType = type.GetDisplayName();
throw new SolutionException(string.Format(Errors.InvalidLoop_Args1, projectType));
throw new SolutionException(string.Format(Errors.InvalidLoop_Args1, projectType), SolutionErrorType.InvalidLoop);
}

currentSlow = this.GetBasedOnType(currentSlow);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace Microsoft.VisualStudio.SolutionPersistence.Model;

public class SolutionArgumentException : ArgumentException
{
public SolutionErrorType Type;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly property?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this should be a readonly property.


public SolutionArgumentException(string? message, SolutionErrorType type)
: base(message)
{
this.Type = type;
}

public SolutionArgumentException(string? message, Exception? innerException, SolutionErrorType type)
: base(message, innerException)
{
this.Type = type;
}

public SolutionArgumentException(string? message, string? paramName, SolutionErrorType type)
: base(message, paramName)
{
this.Type = type;
}

public SolutionArgumentException(string? message, string? paramName, Exception? innerException, SolutionErrorType type)
: base(message, paramName, innerException)
{
this.Type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.SolutionPersistence.Model;

/// <summary>
/// Reasons the SolutionArgumentException was raised.
/// </summary>
public enum SolutionErrorType
{
Undefined,
CannotMoveFolderToChildFolder,
DuplicateDefaultProjectType,
DuplicateExtension,
DuplicateItemRef,
DuplicateName,
DuplicateProjectName,
DuplicateProjectPath,
DuplicateProjectTypeId,
InvalidConfiguration,
InvalidEncoding,
InvalidFolderPath,
InvalidFolderReference,
InvalidItemRef,
InvalidLoop,
InvalidModelItem,
InvalidName,
InvalidProjectReference,
InvalidProjectTypeReference,
InvalidVersion,
MissingProjectValue,
NotSolution,
UnsupportedVersion,
InvalidXmlDecoratorElementName
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ public SolutionException()
/// Initializes a new instance of the <see cref="SolutionException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
public SolutionException(string message)
/// <param name="errorType">The type of error associated to this exception.</param>
public SolutionException(string message, SolutionErrorType errorType = SolutionErrorType.Undefined)
: base(message)
{
this.ErrorType = errorType;
}

/// <summary>
/// Initializes a new instance of the <see cref="SolutionException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="inner">The exception that is the cause of the current exception.</param>
public SolutionException(string message, Exception inner)
/// <param name="errorType">The type of error associated to this exception.</param>
public SolutionException(string message, Exception inner, SolutionErrorType errorType = SolutionErrorType.Undefined)
: base(message, inner)
{
this.ErrorType = errorType;
}

#if NETFRAMEWORK
Expand All @@ -57,6 +61,11 @@ protected SolutionException(System.Runtime.Serialization.SerializationInfo info,
}
#endif

/// <summary>
/// Gets error type.
/// </summary>
public SolutionErrorType? ErrorType { get; init; }

/// <summary>
/// Gets file the error occurred in if known.
/// </summary>
Expand Down Expand Up @@ -84,19 +93,19 @@ public override void GetObjectData(System.Runtime.Serialization.SerializationInf
}
#endif

internal static SolutionException Create(string message, XmlDecorator location)
internal static SolutionException Create(string message, XmlDecorator location, SolutionErrorType errorType = SolutionErrorType.Undefined)
{
return location?.XmlElement is IXmlLineInfo lineInfo && lineInfo.HasLineInfo() ?
new SolutionException(message) { Line = lineInfo.LineNumber, Column = lineInfo.LinePosition, File = location.Root.FullPath } :
new SolutionException(message) { File = location?.Root.FullPath };
new SolutionException(message, errorType) { Line = lineInfo.LineNumber, Column = lineInfo.LinePosition, File = location.Root.FullPath } :
new SolutionException(message, errorType) { File = location?.Root.FullPath };
}

internal static SolutionException Create(Exception innerException, XmlDecorator location, string? message = null)
internal static SolutionException Create(Exception innerException, XmlDecorator location, string? message = null, SolutionErrorType errorType = SolutionErrorType.Undefined)
{
message ??= innerException.Message;
return location?.XmlElement is IXmlLineInfo lineInfo && lineInfo.HasLineInfo() ?
new SolutionException(message, innerException) { Line = lineInfo.LineNumber, Column = lineInfo.LinePosition, File = location.Root.FullPath } :
new SolutionException(message, innerException) { File = location?.Root.FullPath };
new SolutionException(message, innerException, errorType) { Line = lineInfo.LineNumber, Column = lineInfo.LinePosition, File = location.Root.FullPath } :
new SolutionException(message, innerException, errorType) { File = location?.Root.FullPath };
}

// Checks if an exception caught during serialization should be wrapped by a SolutionException to add position information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public string Name
string testName = $"{this.Parent?.ItemRef ?? "/"}{value}/";
if (this.Solution.FindFolder(testName) is not null)
{
throw new ArgumentException(string.Format(Errors.DuplicateItemRef_Args2, testName, "Folder"), nameof(value));
throw new SolutionArgumentException(string.Format(Errors.DuplicateItemRef_Args2, testName, "Folder"), nameof(value), SolutionErrorType.DuplicateItemRef);
}

string oldName = this.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Guid Id
{
if (this.Solution.FindItemById(value) is not null)
{
throw new ArgumentException(string.Format(Errors.DuplicateItemRef_Args2, value, this.GetType().Name), nameof(value));
throw new SolutionArgumentException(string.Format(Errors.DuplicateItemRef_Args2, value, this.GetType().Name), nameof(value), SolutionErrorType.DuplicateItemRef);
}

Guid? oldId = this.id ?? this.defaultId;
Expand Down Expand Up @@ -114,7 +114,7 @@ public void MoveToFolder(SolutionFolderModel? folder)
{
if (ReferenceEquals(parents, this))
{
throw new ArgumentException(Errors.CannotMoveFolderToChildFolder, nameof(folder));
throw new SolutionArgumentException(Errors.CannotMoveFolderToChildFolder, nameof(folder), SolutionErrorType.CannotMoveFolderToChildFolder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public SolutionFolderModel AddFolder(string path)
Argument.ThrowIfNullOrEmpty(path, nameof(path));
if (!path.StartsWith('/') || !path.EndsWith('/'))
{
throw new ArgumentException(string.Format(Errors.InvalidFolderPath_Args1, path), nameof(path));
throw new SolutionArgumentException(string.Format(Errors.InvalidFolderPath_Args1, path), nameof(path), SolutionErrorType.InvalidFolderPath);
}

SolutionFolderModel? existingFolder = this.FindFolder(path);
Expand Down Expand Up @@ -213,7 +213,7 @@ public SolutionProjectModel AddProject(string filePath, string? projectTypeName
Guid projectTypeId =
Guid.TryParse(projectTypeName, out Guid projectTypeGuid) ? projectTypeGuid :
this.ProjectTypeTable.GetProjectTypeId(projectTypeName, Path.GetExtension(filePath.AsSpan())) ??
throw new ArgumentException(string.Format(Errors.InvalidProjectTypeReference_Args1, projectTypeName), nameof(projectTypeName));
throw new SolutionArgumentException(string.Format(Errors.InvalidProjectTypeReference_Args1, projectTypeName), nameof(projectTypeName), SolutionErrorType.InvalidProjectReference);

return this.AddProject(filePath, projectTypeName ?? string.Empty, projectTypeId, folder);
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public bool RemovePlatform(string platform)
Argument.ThrowIfNullOrEmpty(path, nameof(path));
if (!path.StartsWith('/') || !path.EndsWith('/'))
{
throw new ArgumentException(string.Format(Errors.InvalidFolderPath_Args1, path), nameof(path));
throw new SolutionArgumentException(string.Format(Errors.InvalidFolderPath_Args1, path), nameof(path), SolutionErrorType.InvalidFolderPath);
}

return ModelHelper.FindByItemRef(this.solutionFolders, path);
Expand Down Expand Up @@ -386,13 +386,13 @@ internal static void ValidateName(StringSpan name)
{
if (char.IsControl(c) || InvalidNameChars.Contains(c))
{
throw new ArgumentException(Errors.InvalidName, nameof(name));
throw new SolutionArgumentException(Errors.InvalidName, nameof(name), SolutionErrorType.InvalidName);
}
}

if (IsDosWord(name))
{
throw new ArgumentException(Errors.InvalidName, nameof(name));
throw new SolutionArgumentException(Errors.InvalidName, nameof(name), SolutionErrorType.InvalidName);
}

static bool IsDosWord(scoped StringSpan name)
Expand Down Expand Up @@ -491,7 +491,7 @@ internal SolutionProjectModel AddProject(string filePath, string projectTypeName
// Project is already in the solution.
if (this.FindProject(project.FilePath) is not null)
{
throw new ArgumentException(string.Format(Errors.DuplicateProjectPath_Arg1, project.ItemRef), nameof(filePath));
throw new SolutionArgumentException(string.Format(Errors.DuplicateProjectPath_Arg1, project.ItemRef), nameof(filePath), SolutionErrorType.DuplicateProjectPath);
}

this.ValidateProjectName(project);
Expand Down Expand Up @@ -602,7 +602,7 @@ internal void ValidateProjectName(SolutionProjectModel project)

if (existingProject.ActualDisplayName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException(string.Format(Errors.DuplicateProjectName_Arg1, displayName));
throw new SolutionArgumentException(string.Format(Errors.DuplicateProjectName_Arg1, displayName), SolutionErrorType.DuplicateProjectName);
}
}
}
Expand All @@ -611,7 +611,7 @@ internal void ValidateInModel(SolutionItemModel? item)
{
if (item is not null && item.Solution != this)
{
throw new ArgumentException(Errors.InvalidModelItem, nameof(item));
throw new SolutionArgumentException(Errors.InvalidModelItem, nameof(item), SolutionErrorType.InvalidModelItem);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public string FilePath
{
if (this.Solution.FindProject(value) is not null)
{
throw new ArgumentException(string.Format(Errors.DuplicateItemRef_Args2, value, "Project"), nameof(value));
throw new SolutionArgumentException(string.Format(Errors.DuplicateItemRef_Args2, value, "Project"), nameof(value), SolutionErrorType.DuplicateItemRef);
}

string oldPath = this.filePath!;
Expand Down Expand Up @@ -211,7 +211,7 @@ public void AddDependency(SolutionProjectModel dependency)

if (ReferenceEquals(dependency, this))
{
throw new ArgumentException(string.Format(Errors.InvalidLoop_Args1, dependency.ItemRef), nameof(dependency));
throw new SolutionArgumentException(string.Format(Errors.InvalidLoop_Args1, dependency.ItemRef), nameof(dependency), SolutionErrorType.InvalidLoop);
}

this.dependencies ??= [];
Expand Down
Loading