Made a helper method to check if an object is an error

This commit is contained in:
Kir_Antipov 2023-01-09 15:21:43 +00:00
parent 5892ef60d8
commit 3fe2a3fdc9
2 changed files with 21 additions and 0 deletions

10
src/utils/errors/error.ts Normal file
View file

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

View file

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