mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-25 01:41:05 -05:00
Added message
parameter to throw helpers
This commit is contained in:
parent
f7940da40c
commit
dde2a04f94
6 changed files with 21 additions and 6 deletions
|
@ -54,12 +54,13 @@ export class ArgumentError extends Error {
|
||||||
*
|
*
|
||||||
* @param argument - The argument to check.
|
* @param argument - The argument to check.
|
||||||
* @param paramName - The name of the parameter being checked.
|
* @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.
|
* @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) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,12 +27,13 @@ export class ArgumentNullError extends ArgumentError {
|
||||||
*
|
*
|
||||||
* @param argument - The argument to check.
|
* @param argument - The argument to check.
|
||||||
* @param paramName - The name of the parameter being checked.
|
* @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`.
|
* @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) {
|
if (argument === undefined || argument === null) {
|
||||||
throw new ArgumentNullError(paramName);
|
throw new ArgumentNullError(paramName, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,11 @@ export class FileNotFoundError extends Error {
|
||||||
* Throws a {@link FileNotFoundError} if the specified file does not exist.
|
* Throws a {@link FileNotFoundError} if the specified file does not exist.
|
||||||
*
|
*
|
||||||
* @param fileName - The name of the file to check for existence.
|
* @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)) {
|
if (!existsSync(fileName)) {
|
||||||
throw new FileNotFoundError(String(fileName));
|
throw new FileNotFoundError(String(fileName), message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,10 @@ describe("ArgumentError", () => {
|
||||||
expect(() => ArgumentError.throwIfNullOrEmpty("", "param1")).toThrowError(new ArgumentError("param1", "The value cannot be null, undefined, or empty."));
|
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", () => {
|
test("does not throw if the argument is not null, undefined, or empty", () => {
|
||||||
expect(() => ArgumentError.throwIfNullOrEmpty("not empty", "param1")).not.toThrow();
|
expect(() => ArgumentError.throwIfNullOrEmpty("not empty", "param1")).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
|
@ -34,6 +34,10 @@ describe("ArgumentNullError", () => {
|
||||||
expect(() => ArgumentNullError.throwIfNull(undefined, "param1")).toThrowError(new ArgumentNullError("param1"));
|
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", () => {
|
test("does not throw if the argument is not null or undefined", () => {
|
||||||
expect(() => ArgumentNullError.throwIfNull("not null or undefined", "param1")).not.toThrow();
|
expect(() => ArgumentNullError.throwIfNull("not null or undefined", "param1")).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
|
@ -35,6 +35,10 @@ describe("FileNotFoundError", () => {
|
||||||
expect(() => FileNotFoundError.throwIfNotFound("test.txt")).toThrow(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", () => {
|
test("does not throw error if file exists", () => {
|
||||||
expect(() => FileNotFoundError.throwIfNotFound("test")).not.toThrow();
|
expect(() => FileNotFoundError.throwIfNotFound("test")).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue