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 EMake 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: unknownThe error to assert.
ErrorClass?: new (...args: any[]) => EThe optional error class to assert.
msgMatches?: string | RegExpThe optional string or RegExp to assert in the error message.
msg?: stringThe 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