assertIsError

import { assertIsError } from "jsr:@ende/assert";

function

function assertIsError<E extends Error = Error>(error: unknown, ErrorClass?: new (...args: any[]) => E, msgMatches?: string | RegExp, msg?: string): asserts error is E

Make an assertion that error is an Error. If not then an error will be thrown. An error class and a string that should be included in the error message can also be asserted.

error: unknown

The error to assert.

ErrorClass?: new (...args: any[]) => E

The optional error class to assert.

msgMatches?: string | RegExp

The optional string or RegExp to assert in the error message.

msg?: string

The optional message to display if the assertion fails.

Example: Usage

import { assertIsError } from "@ende/assert";

assertIsError(null); // Throws
assertIsError(new RangeError("Out of range")); // Doesn't throw
assertIsError(new RangeError("Out of range"), SyntaxError); // Throws
assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw
assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws

Source