From 8f97d4aee69b00efe6b607f94b6d14a5d95e8233 Mon Sep 17 00:00:00 2001 From: Kir_Antipov <kp.antipov@gmail.com> Date: Sat, 7 Jan 2023 08:52:43 +0000 Subject: [PATCH] Implemented `ArgumentNullError` --- src/utils/errors/argument-null-error.ts | 38 +++++++++++++++++ .../errors.ts/argument-null-error.spec.ts | 41 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/utils/errors/argument-null-error.ts create mode 100644 tests/unit/utils/errors.ts/argument-null-error.spec.ts diff --git a/src/utils/errors/argument-null-error.ts b/src/utils/errors/argument-null-error.ts new file mode 100644 index 0000000..086ec60 --- /dev/null +++ b/src/utils/errors/argument-null-error.ts @@ -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); + } + } +} diff --git a/tests/unit/utils/errors.ts/argument-null-error.spec.ts b/tests/unit/utils/errors.ts/argument-null-error.spec.ts new file mode 100644 index 0000000..83a819a --- /dev/null +++ b/tests/unit/utils/errors.ts/argument-null-error.spec.ts @@ -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(); + }); + }); +});