Skip to content

Commit

Permalink
refactor: use CommonExceptions to create exceptions (#1027)
Browse files Browse the repository at this point in the history
Replace the creation of exceptions to throw from a couple of places to the static `CommonExceptions` helper class.
  • Loading branch information
vbreuss authored Aug 22, 2023
1 parent 34ae96a commit 0755df0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace System.IO.Abstractions.TestingHelpers
internal static class CommonExceptions
{
private const int _fileLockHResult = unchecked((int)0x80070020);

public static FileNotFoundException FileNotFound(string path) =>
new FileNotFoundException(
string.Format(
Expand Down Expand Up @@ -34,10 +34,10 @@ public static UnauthorizedAccessException AccessDenied(string path) =>
)
);

public static Exception InvalidUseOfVolumeSeparator() =>
public static NotSupportedException InvalidUseOfVolumeSeparator() =>
new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"));

public static Exception PathIsNotOfALegalForm(string paramName) =>
public static ArgumentException PathIsNotOfALegalForm(string paramName) =>
new ArgumentException(
StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"),
paramName
Expand All @@ -54,7 +54,7 @@ public static ArgumentException IllegalCharactersInPath(string paramName = null)
? new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), paramName)
: new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));

public static Exception InvalidUncPath(string paramName) =>
public static ArgumentException InvalidUncPath(string paramName) =>
new ArgumentException(@"The UNC path should be of the form \\server\share.", paramName);

public static IOException ProcessCannotAccessFileInUse(string paramName = null) =>
Expand All @@ -73,5 +73,32 @@ public static ArgumentException AppendAccessOnlyInWriteOnlyMode()

public static NotImplementedException NotImplemented() =>
new NotImplementedException(StringResources.Manager.GetString("NOT_IMPLEMENTED_EXCEPTION"));

public static IOException CannotCreateBecauseSameNameAlreadyExists(string path) =>
new IOException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("CANNOT_CREATE_BECAUSE_SAME_NAME_ALREADY_EXISTS"),
path
)
);

public static IOException NameCannotBeResolvedByTheSystem(string path) =>
new IOException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("NAME_CANNOT_BE_RESOLVED_BY_THE_SYSTEM"),
path
)
);

public static DirectoryNotFoundException PathDoesNotExistOrCouldNotBeFound(string path) =>
new DirectoryNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("PATH_DOES_NOT_EXIST_OR_COULD_NOT_BE_FOUND"),
path
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public override void Delete(string path, bool recursive)

if (!affectedPaths.Any())
{
throw new DirectoryNotFoundException(path + " does not exist or could not be found.");
throw CommonExceptions.PathDoesNotExistOrCouldNotBeFound(path);
}

if (!recursive && affectedPaths.Count > 1)
Expand Down Expand Up @@ -526,18 +526,17 @@ public override void Move(string sourceDirName, string destDirName)

if (!mockFileDataAccessor.Directory.Exists(fullSourcePath))
{
throw new DirectoryNotFoundException($"Could not find a part of the path '{sourceDirName}'.");
throw CommonExceptions.CouldNotFindPartOfPath(sourceDirName);
}

if (!mockFileDataAccessor.Directory.GetParent(fullDestPath).Exists)
{
throw new DirectoryNotFoundException($"Could not find a part of the path.");
throw CommonExceptions.CouldNotFindPartOfPath(destDirName);
}

if (mockFileDataAccessor.Directory.Exists(fullDestPath) || mockFileDataAccessor.File.Exists(fullDestPath))
{
throw new IOException(
$"Cannot create '{fullDestPath}' because a file or directory with the same name already exists.");
throw CommonExceptions.CannotCreateBecauseSameNameAlreadyExists(fullDestPath);
}

mockFileDataAccessor.MoveDirectory(fullSourcePath, fullDestPath);
Expand Down Expand Up @@ -570,7 +569,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi

if (nextContainer.LinkTarget != null)
{
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
}

Expand All @@ -583,7 +582,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi
return new MockFileInfo(mockFileDataAccessor, nextLocation);
}
}
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public override void Copy(string sourceFileName, string destFileName, bool overw
{
if (!overwrite)
{
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file {0} already exists.", destFileName));
throw CommonExceptions.FileAlreadyExists(destFileName);
}

mockFileDataAccessor.RemoveFile(destFileName);
Expand Down Expand Up @@ -581,7 +581,7 @@ private FileSystemStream OpenInternal(

if (mode == FileMode.CreateNew && exists)
{
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' already exists.", path));
throw CommonExceptions.FileAlreadyExists(path);
}

if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists)
Expand Down Expand Up @@ -802,7 +802,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi

if (nextContainer.LinkTarget != null)
{
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
}

Expand All @@ -815,7 +815,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi
return new MockFileInfo(mockFileDataAccessor, nextLocation);
}
}
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,13 @@
<data name="INVALID_ACCESS_COMBINATION" xml:space="preserve">
<value>Combining FileMode: {0} with FileAccess: {1} is invalid.</value>
</data>
<data name="CANNOT_CREATE_BECAUSE_SAME_NAME_ALREADY_EXISTS" xml:space="preserve">
<value>Cannot create '{0}' because a file or directory with the same name already exists.</value>
</data>
<data name="NAME_CANNOT_BE_RESOLVED_BY_THE_SYSTEM" xml:space="preserve">
<value>The name of the file cannot be resolved by the system. : '{0}'</value>
</data>
<data name="PATH_DOES_NOT_EXIST_OR_COULD_NOT_BE_FOUND" xml:space="preserve">
<value>'{0}' does not exist or could not be found.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ public void MockDirectory_Delete_ShouldThrowDirectoryNotFoundException()

var ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.Directory.Delete(XFS.Path(@"c:\baz")));

Assert.That(ex.Message, Is.EqualTo(XFS.Path("c:\\baz") + " does not exist or could not be found."));
Assert.That(ex.Message, Is.EqualTo($"'{XFS.Path("c:\\baz")}' does not exist or could not be found."));
}

[Test]
Expand Down

0 comments on commit 0755df0

Please # to comment.