Function assertIsError

  • 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.

    Type Parameters

    • E extends Error = Error

      The type of the error to assert.

    Parameters

    • error: unknown

      The error to assert.

    • OptionalErrorClass: (new (...args: any[]) => E)

      The optional error class to assert.

        • new (...args): E
        • Parameters

          • Rest...args: any[]

          Returns E

    • OptionalmsgMatches: string | RegExp

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

    • Optionalmsg: string

      The optional message to display if the assertion fails.

    Returns asserts error is E

    import { assertIsError } from "@std/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