diff --git a/src/utils/errors/argument-error.ts b/src/utils/errors/argument-error.ts index cf004a1..a3db965 100644 --- a/src/utils/errors/argument-error.ts +++ b/src/utils/errors/argument-error.ts @@ -54,12 +54,13 @@ export class ArgumentError extends Error { * * @param argument - The argument to check. * @param paramName - The name of the parameter being checked. + * @param message - The error message to display. * * @throws An {@link ArgumentError} if the specified argument is `null`, `undefined`, or empty. */ - static throwIfNullOrEmpty(argument?: { length: number }, paramName?: string): void | never { + static throwIfNullOrEmpty(argument?: { length: number }, paramName?: string, message?: string): void | never { if (argument === undefined || argument === null || argument.length === 0) { - throw new ArgumentError(paramName, ArgumentError.EMPTY_ARGUMENT_ERROR_MESSAGE); + throw new ArgumentError(paramName, message || ArgumentError.EMPTY_ARGUMENT_ERROR_MESSAGE); } } diff --git a/src/utils/errors/argument-null-error.ts b/src/utils/errors/argument-null-error.ts index 086ec60..c36df1a 100644 --- a/src/utils/errors/argument-null-error.ts +++ b/src/utils/errors/argument-null-error.ts @@ -27,12 +27,13 @@ export class ArgumentNullError extends ArgumentError { * * @param argument - The argument to check. * @param paramName - The name of the parameter being checked. + * @param message - The error message to display. * * @throws An {@link ArgumentNullError} if the specified argument is `null` or `undefined`. */ - static throwIfNull(argument?: unknown, paramName?: string): void | never { + static throwIfNull(argument?: unknown, paramName?: string, message?: string): void | never { if (argument === undefined || argument === null) { - throw new ArgumentNullError(paramName); + throw new ArgumentNullError(paramName, message); } } } diff --git a/src/utils/errors/file-not-found-error.ts b/src/utils/errors/file-not-found-error.ts index 8ec6a45..1e7c5fb 100644 --- a/src/utils/errors/file-not-found-error.ts +++ b/src/utils/errors/file-not-found-error.ts @@ -39,10 +39,11 @@ export class FileNotFoundError extends Error { * Throws a {@link FileNotFoundError} if the specified file does not exist. * * @param fileName - The name of the file to check for existence. + * @param message - The error message to display. */ - static throwIfNotFound(fileName: PathLike): void | never { + static throwIfNotFound(fileName: PathLike, message?: string): void | never { if (!existsSync(fileName)) { - throw new FileNotFoundError(String(fileName)); + throw new FileNotFoundError(String(fileName), message); } } } diff --git a/tests/unit/utils/errors/argument-error.spec.ts b/tests/unit/utils/errors/argument-error.spec.ts index 8bfa70f..b3f1793 100644 --- a/tests/unit/utils/errors/argument-error.spec.ts +++ b/tests/unit/utils/errors/argument-error.spec.ts @@ -38,6 +38,10 @@ describe("ArgumentError", () => { expect(() => ArgumentError.throwIfNullOrEmpty("", "param1")).toThrowError(new ArgumentError("param1", "The value cannot be null, undefined, or empty.")); }); + test("throws an ArgumentError with the provided error message", () => { + expect(() => ArgumentError.throwIfNullOrEmpty(null, "param1", "I don't like nulls.")).toThrowError(new ArgumentError("param1", "I don't like nulls.")); + }); + test("does not throw if the argument is not null, undefined, or empty", () => { expect(() => ArgumentError.throwIfNullOrEmpty("not empty", "param1")).not.toThrow(); }); diff --git a/tests/unit/utils/errors/argument-null-error.spec.ts b/tests/unit/utils/errors/argument-null-error.spec.ts index 83a819a..1e54843 100644 --- a/tests/unit/utils/errors/argument-null-error.spec.ts +++ b/tests/unit/utils/errors/argument-null-error.spec.ts @@ -34,6 +34,10 @@ describe("ArgumentNullError", () => { expect(() => ArgumentNullError.throwIfNull(undefined, "param1")).toThrowError(new ArgumentNullError("param1")); }); + test("throws an ArgumentNullError with the provided error message", () => { + expect(() => ArgumentNullError.throwIfNullOrEmpty(null, "param1", "I don't like nulls.")).toThrowError(new ArgumentNullError("param1", "I don't like nulls.")); + }); + test("does not throw if the argument is not null or undefined", () => { expect(() => ArgumentNullError.throwIfNull("not null or undefined", "param1")).not.toThrow(); }); diff --git a/tests/unit/utils/errors/file-not-found-error.spec.ts b/tests/unit/utils/errors/file-not-found-error.spec.ts index 8cd177d..027866a 100644 --- a/tests/unit/utils/errors/file-not-found-error.spec.ts +++ b/tests/unit/utils/errors/file-not-found-error.spec.ts @@ -35,6 +35,10 @@ describe("FileNotFoundError", () => { expect(() => FileNotFoundError.throwIfNotFound("test.txt")).toThrow(FileNotFoundError); }); + test("throws a FileNotFoundError with the provided error message", () => { + expect(() => FileNotFoundError.throwIfNotFound("test.txt", "I don't like file extensions.")).toThrowError(new FileNotFoundError("test.txt", "I don't like file extensions.")); + }); + test("does not throw error if file exists", () => { expect(() => FileNotFoundError.throwIfNotFound("test")).not.toThrow(); });