From 3fe2a3fdc9a47b7c13eacee1cfd7595d68c71bf9 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Mon, 9 Jan 2023 15:21:43 +0000 Subject: [PATCH] Made a helper method to check if an object is an error --- src/utils/errors/error.ts | 10 ++++++++++ tests/unit/utils/errors.ts/error.spec.ts | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/utils/errors/error.ts create mode 100644 tests/unit/utils/errors.ts/error.spec.ts diff --git a/src/utils/errors/error.ts b/src/utils/errors/error.ts new file mode 100644 index 0000000..515dc5a --- /dev/null +++ b/src/utils/errors/error.ts @@ -0,0 +1,10 @@ +/** + * Determines if the input is an {@link Error}. + * + * @param error - Input to be checked. + * + * @returns `true` if the input is an `Error`; otherwise, `false`. + */ +export function isError(error: unknown): error is Error { + return error instanceof Error; +} diff --git a/tests/unit/utils/errors.ts/error.spec.ts b/tests/unit/utils/errors.ts/error.spec.ts new file mode 100644 index 0000000..46ed6e2 --- /dev/null +++ b/tests/unit/utils/errors.ts/error.spec.ts @@ -0,0 +1,11 @@ +import { isError } from "@/utils/errors/error"; + +describe("isError", () => { + test("returns true if the input is an instance of Error", () => { + expect(isError(new Error("test error"))).toBe(true); + }); + + test("returns false if the input is not an instance of Error", () => { + expect(isError("not an error")).toBe(false); + }); +});