Skip to content

Commit 120be7c

Browse files
author
LoneWandererProductions
committed
Fix up the last pieces of Filehandler
1 parent 73a7008 commit 120be7c

7 files changed

Lines changed: 273 additions & 265 deletions

File tree

CommonLibraryTests/HelperMethods.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,18 @@ internal static void CreateFile(string path)
6060
{
6161
var folder = Path.GetDirectoryName(path);
6262
if (folder == null)
63-
{
64-
return;
65-
}
63+
throw new ArgumentException("Path must contain a directory.", nameof(path));
6664

67-
if (!Directory.Exists(folder))
68-
{
69-
_ = Directory.CreateDirectory(folder);
70-
}
65+
Directory.CreateDirectory(folder); // safe even if exists
7166

7267
if (File.Exists(path))
73-
{
74-
return;
75-
}
68+
File.Delete(path);
7669

7770
using var fs = File.Create(path);
7871
for (byte i = 0; i < 100; i++)
79-
{
8072
fs.WriteByte(i);
81-
}
73+
74+
fs.Flush(true); // ensure fully written to disk
8275
}
8376

8477
/// <summary>

CommonLibraryTests/IoFileHandlerInternal.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void CleanUpExtensionListValidInputRemovesDots()
5353
/// Gets the sub folder invalid element path throws argument exception.
5454
/// </summary>
5555
[TestMethod]
56-
[ExpectedException(typeof(ArgumentNullException))]
56+
[ExpectedException(typeof(ArgumentException))]
5757
public void GetSubFolderInvalidElementPathThrowsArgumentException()
5858
{
5959
// Arrange
@@ -69,7 +69,7 @@ public void GetSubFolderInvalidElementPathThrowsArgumentException()
6969
/// Gets the sub folder invalid root path throws argument exception.
7070
/// </summary>
7171
[TestMethod]
72-
[ExpectedException(typeof(ArgumentNullException))]
72+
[ExpectedException(typeof(ArgumentException))]
7373
public void GetSubFolderInvalidRootPathThrowsArgumentException()
7474
{
7575
// Arrange

FileHandler/FileHandleSort.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static class FileHandleSort
3838
return new List<T>();
3939

4040
return values
41-
.OrderBy(v => new FilePathStruct(pathSelector(v)))
41+
.OrderBy(v => new FilePathWrapper(pathSelector(v)))
4242
.ToList();
4343
}
4444

@@ -57,9 +57,9 @@ public static List<string> PathSort(this List<string>? value)
5757
return value ?? new List<string>();
5858

5959
return value
60-
.Select(path => new FilePathStruct(path))
60+
.Select(path => new FilePathWrapper(path))
6161
.OrderBy(fps => fps)
62-
.Select(fps => fps.Path)
62+
.Select(fps => fps.FullPath)
6363
.ToList();
6464
}
6565
}

FileHandler/FileHandlerException.cs

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,77 @@
33
* PROJECT: FileHandler
44
* FILE: FileHandler/FileHandlerException.cs
55
* PURPOSE: Exception Class
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
7-
* SOURCES: https://msdn.microsoft.com/en-us/library/system.exception.getobjectdata.aspx
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
87
*/
98

109
using System;
11-
using System.Runtime.Serialization;
1210

13-
namespace FileHandler;
14-
15-
/// <inheritdoc />
16-
/// <summary>
17-
/// The file handler exception class.
18-
/// </summary>
19-
/// <seealso cref="T:System.Exception" />
20-
[Serializable]
21-
public sealed class FileHandlerException : Exception
11+
namespace FileHandler
2212
{
23-
/// <inheritdoc />
2413
/// <summary>
25-
/// Initializes a new instance of the <see cref="T:FileHandler.FileHandlerException" /> class.
14+
/// Represents errors that occur within the <c>FileHandler</c> library.
2615
/// </summary>
27-
/// <param name="message">The message.</param>
28-
internal FileHandlerException(string message) : base(message)
16+
/// <remarks>
17+
/// This exception is thrown for library-specific errors, providing a consistent type
18+
/// for users to catch and handle separately from standard .NET exceptions.
19+
/// </remarks>
20+
public sealed class FileHandlerException : Exception
2921
{
30-
}
22+
/// <summary>
23+
/// Gets the path of the file associated with this exception, if any.
24+
/// </summary>
25+
public string? FilePath { get; }
3126

32-
/// <inheritdoc />
33-
/// <summary>
34-
/// Initializes a new instance of the <see cref="T:FileHandler.FileHandlerException" /> class.
35-
/// </summary>
36-
/// <param name="info">The info.</param>
37-
/// <param name="context">The context.</param>
38-
private FileHandlerException(SerializationInfo info, StreamingContext context) : base(info, context)
39-
{
40-
}
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="FileHandlerException"/> class.
29+
/// </summary>
30+
public FileHandlerException()
31+
{
32+
}
4133

42-
/// <inheritdoc />
43-
/// <summary>
44-
/// Initializes a new instance of the <see cref="T:FileHandler.FileHandlerException" /> class.
45-
/// </summary>
46-
public FileHandlerException()
47-
{
48-
}
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="FileHandlerException"/> class with a specified error message.
36+
/// </summary>
37+
/// <param name="message">The message describing the error.</param>
38+
public FileHandlerException(string message)
39+
: base(message)
40+
{
41+
}
4942

50-
/// <inheritdoc />
51-
/// <summary>
52-
/// Initializes a new instance of the <see cref="T:FileHandler.FileHandlerException" /> class.
53-
/// </summary>
54-
/// <param name="message">The message we declarte</param>
55-
/// <param name="innerException">
56-
/// The Exception that caused the Exception or a null reference <see langword="Nothing" /> in
57-
/// Visual Basic), if there is no inner Exception.
58-
/// </param>
59-
public FileHandlerException(string message, Exception innerException) : base(message, innerException)
60-
{
43+
/// <summary>
44+
/// Initializes a new instance of the <see cref="FileHandlerException"/> class with a specified
45+
/// error message and a reference to the inner exception that caused this exception.
46+
/// </summary>
47+
/// <param name="message">The message describing the error.</param>
48+
/// <param name="innerException">The exception that caused the current exception.</param>
49+
public FileHandlerException(string message, Exception innerException)
50+
: base(message, innerException)
51+
{
52+
}
53+
54+
/// <summary>
55+
/// Initializes a new instance of the <see cref="FileHandlerException"/> class with a specified
56+
/// error message and an associated file path.
57+
/// </summary>
58+
/// <param name="message">The message describing the error.</param>
59+
/// <param name="filePath">The file path related to the error.</param>
60+
public FileHandlerException(string message, string filePath)
61+
: base(message)
62+
{
63+
FilePath = filePath;
64+
}
65+
66+
/// <summary>
67+
/// Initializes a new instance of the <see cref="FileHandlerException"/> class with a specified
68+
/// error message, associated file path, and a reference to the inner exception that caused this exception.
69+
/// </summary>
70+
/// <param name="message">The message describing the error.</param>
71+
/// <param name="filePath">The file path related to the error.</param>
72+
/// <param name="innerException">The exception that caused the current exception.</param>
73+
public FileHandlerException(string message, string filePath, Exception innerException)
74+
: base(message, innerException)
75+
{
76+
FilePath = filePath;
77+
}
6178
}
6279
}

0 commit comments

Comments
 (0)