Skip to content

AssertError

Defined in: errors.ts:21

The Error subclass thrown by assertNotNull. Its name is "AssertError", so you can tell a failed assertion apart from other failures with instanceof (or by error.name). Throw it from your own checks too.

Tell a failed assertion apart from other errors

import { AssertError, assertNotNull, tryCatch } from "@xtandard/lib";
const [, error] = tryCatch(() => assertNotNull(null));
error instanceof AssertError; // true
String(error); // "AssertError: unexpected null or undefined"

Throw it from your own check

if (rows.length === 0) throw new AssertError("expected at least one row");
  • Error
new AssertError(message?): AssertError;

Defined in: errors.ts:34

Creates an assertion error with the given message.

Parameter Type Default value Description
message? string "unexpected null or undefined" The error message. Defaults to "unexpected null or undefined", the same default as assertNotNull.

AssertError

new AssertError().message; // "unexpected null or undefined"
new AssertError("expected at least one row").name; // "AssertError"

Error.constructor