Added message parameter to throw helpers

This commit is contained in:
Kir_Antipov 2024-01-14 22:27:10 +03:00
parent f7940da40c
commit dde2a04f94
6 changed files with 21 additions and 6 deletions

View file

@ -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);
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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();
});

View file

@ -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();
});

View file

@ -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();
});