mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Implemented ArgumentNullError
This commit is contained in:
parent
ab8f3468ca
commit
8f97d4aee6
2 changed files with 79 additions and 0 deletions
38
src/utils/errors/argument-null-error.ts
Normal file
38
src/utils/errors/argument-null-error.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { ArgumentError } from "./argument-error";
|
||||
|
||||
/**
|
||||
* Represents an error that occurs when a required argument is null or undefined.
|
||||
*/
|
||||
export class ArgumentNullError extends ArgumentError {
|
||||
/**
|
||||
* The default message to use when no message is provided.
|
||||
*/
|
||||
private static readonly DEFAULT_ARGUMENT_NULL_ERROR_MESSAGE = "Value cannot be null or undefined.";
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@link ArgumentNullError} class.
|
||||
*
|
||||
* @param paramName - The name of the parameter that caused the error.
|
||||
* @param message - The error message to display.
|
||||
* @param options - Optional settings for the error object.
|
||||
*/
|
||||
constructor(paramName?: string, message?: string, options?: ErrorOptions) {
|
||||
super(paramName, message ?? ArgumentNullError.DEFAULT_ARGUMENT_NULL_ERROR_MESSAGE, options);
|
||||
|
||||
this.name = "ArgumentNullError";
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an {@link ArgumentNullError} if the specified argument is `null` or `undefined`.
|
||||
*
|
||||
* @param argument - The argument to check.
|
||||
* @param paramName - The name of the parameter being checked.
|
||||
*
|
||||
* @throws An {@link ArgumentNullError} if the specified argument is `null` or `undefined`.
|
||||
*/
|
||||
static throwIfNull(argument?: unknown, paramName?: string): void | never {
|
||||
if (argument === undefined || argument === null) {
|
||||
throw new ArgumentNullError(paramName);
|
||||
}
|
||||
}
|
||||
}
|
41
tests/unit/utils/errors.ts/argument-null-error.spec.ts
Normal file
41
tests/unit/utils/errors.ts/argument-null-error.spec.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { ArgumentNullError } from "@/utils/errors/argument-null-error";
|
||||
|
||||
describe("ArgumentNullError", () => {
|
||||
describe("constructor", () => {
|
||||
test("creates an instance with the given parameter name and message", () => {
|
||||
const error = new ArgumentNullError("param1", "test message");
|
||||
|
||||
expect(error).toBeInstanceOf(ArgumentNullError);
|
||||
expect(error.name).toBe("ArgumentNullError");
|
||||
expect(error.message).toBe("test message (Parameter 'param1')");
|
||||
expect(error.paramName).toBe("param1");
|
||||
});
|
||||
|
||||
test("creates an instance with a default message if no message is provided", () => {
|
||||
const error = new ArgumentNullError("param1");
|
||||
|
||||
expect(error.message).toBe("Value cannot be null or undefined. (Parameter 'param1')");
|
||||
});
|
||||
|
||||
test("creates an instance with no parameter name if none is provided", () => {
|
||||
const error = new ArgumentNullError(undefined, "test message");
|
||||
|
||||
expect(error.message).toBe("test message");
|
||||
expect(error.paramName).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("throwIfNull", () => {
|
||||
test("throws an ArgumentNullError with a specified parameter name if the argument is null", () => {
|
||||
expect(() => ArgumentNullError.throwIfNull(null, "param1")).toThrowError(new ArgumentNullError("param1"));
|
||||
});
|
||||
|
||||
test("throws an ArgumentNullError with a specified parameter name if the argument is undefined", () => {
|
||||
expect(() => ArgumentNullError.throwIfNull(undefined, "param1")).toThrowError(new ArgumentNullError("param1"));
|
||||
});
|
||||
|
||||
test("does not throw if the argument is not null or undefined", () => {
|
||||
expect(() => ArgumentNullError.throwIfNull("not null or undefined", "param1")).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue