Skip to content

API documentation of Agoric SDK / Exports / @agoric/assert / types

Module: types

Table of contents

Interfaces

Type Aliases

Type Aliases

Assert

Ƭ Assert: BaseAssert & { Fail: FailTag ; bare: AssertQuote ; details: DetailsTag ; equal: AssertEqual ; error: AssertMakeError ; fail: AssertFail ; makeAssert: MakeAssert ; note: AssertNote ; quote: AssertQuote ; string: AssertString ; typeof: AssertTypeof }

Defined in

types.js:350


AssertEqual

Ƭ AssertEqual: (actual: any, expected: any, optDetails?: Details, ErrorConstructor?: ErrorConstructor) => void

The assert.equal method

Assert that two values must be Object.is.

Type declaration

▸ (actual, expected, optDetails?, ErrorConstructor?): void

Parameters
NameTypeDescription
actualanyThe value we received
expectedanyWhat we wanted
optDetails?DetailsThe details to throw
ErrorConstructor?ErrorConstructorAn optional alternate error constructor to use.
Returns

void

Defined in

types.js:57


AssertFail

Ƭ AssertFail: (optDetails?: Details, ErrorConstructor?: ErrorConstructor) => never

The assert.fail method.

Fail an assertion, recording full details to the console and raising an exception with a message in which details substitution values have been masked.

The optional optDetails can be a string for backwards compatibility with the nodejs assertion library.

Type declaration

▸ (optDetails?, ErrorConstructor?): never

Parameters
NameTypeDescription
optDetails?DetailsThe details of what was asserted
ErrorConstructor?ErrorConstructorAn optional alternate error constructor to use.
Returns

never

Defined in

types.js:40


AssertMakeError

Ƭ AssertMakeError: (optDetails?: Details, ErrorConstructor?: ErrorConstructor, options?: AssertMakeErrorOptions) => Error

The makeError function, recording details for the console.

The optional optDetails can be a string.

Type declaration

▸ (optDetails?, ErrorConstructor?, options?): Error

Parameters
NameTypeDescription
optDetails?DetailsThe details of what was asserted
ErrorConstructor?ErrorConstructorAn optional alternate error constructor to use.
options?AssertMakeErrorOptions-
Returns

Error

Defined in

types.js:27


AssertNote

Ƭ AssertNote: (error: Error, detailsNote: Details) => void

The errorNote function.

Annotate an error with details, potentially to be used by an augmented console such as the causal console of console.js, to provide extra information associated with logged errors.

Type declaration

▸ (error, detailsNote): void

Parameters
NameType
errorError
detailsNoteDetails
Returns

void

Defined in

types.js:156


AssertQuote

Ƭ AssertQuote: (payload: any, spaces?: string | number) => StringablePayload

Type declaration

▸ (payload, spaces?): StringablePayload

Parameters
NameTypeDescription
payloadanyWhat to declassify
spaces?string | number-
Returns

StringablePayload

Defined in

types.js:209


AssertString

Ƭ AssertString: (specimen: any, optDetails?: Details) => asserts specimen is string

The assert.string method.

assert.string(v) is equivalent to assert.typeof(v, 'string'). We special case this one because it is the most frequently used.

Assert an expected typeof result.

Type declaration

▸ (specimen, optDetails?): asserts specimen is string

Parameters
NameTypeDescription
specimenanyThe value to get the typeof
optDetails?DetailsThe details to throw
Returns

asserts specimen is string

Defined in

types.js:143


AssertTypeof

Ƭ AssertTypeof: AssertTypeofBigint & AssertTypeofBoolean & AssertTypeofFunction & AssertTypeofNumber & AssertTypeofObject & AssertTypeofString & AssertTypeofSymbol & AssertTypeofUndefined

Defined in

types.js:139


AssertTypeofBigint

Ƭ AssertTypeofBigint: (specimen: any, typename: "bigint", optDetails?: Details) => asserts specimen is bigint

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is bigint

Parameters
NameType
specimenany
typename"bigint"
optDetails?Details
Returns

asserts specimen is bigint

Defined in

types.js:73


AssertTypeofBoolean

Ƭ AssertTypeofBoolean: (specimen: any, typename: "boolean", optDetails?: Details) => asserts specimen is boolean

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is boolean

Parameters
NameType
specimenany
typename"boolean"
optDetails?Details
Returns

asserts specimen is boolean

Defined in

types.js:81


AssertTypeofFunction

Ƭ AssertTypeofFunction: (specimen: any, typename: "function", optDetails?: Details) => asserts specimen is Function

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is Function

Parameters
NameType
specimenany
typename"function"
optDetails?Details
Returns

asserts specimen is Function

Defined in

types.js:89


AssertTypeofNumber

Ƭ AssertTypeofNumber: (specimen: any, typename: "number", optDetails?: Details) => asserts specimen is number

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is number

Parameters
NameType
specimenany
typename"number"
optDetails?Details
Returns

asserts specimen is number

Defined in

types.js:97


AssertTypeofObject

Ƭ AssertTypeofObject: (specimen: any, typename: "object", optDetails?: Details) => asserts specimen is Record<any, any> | null

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is Record<any, any> | null

Parameters
NameType
specimenany
typename"object"
optDetails?Details
Returns

asserts specimen is Record<any, any> | null

Defined in

types.js:105


AssertTypeofString

Ƭ AssertTypeofString: (specimen: any, typename: "string", optDetails?: Details) => asserts specimen is string

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is string

Parameters
NameType
specimenany
typename"string"
optDetails?Details
Returns

asserts specimen is string

Defined in

types.js:113


AssertTypeofSymbol

Ƭ AssertTypeofSymbol: (specimen: any, typename: "symbol", optDetails?: Details) => asserts specimen is symbol

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is symbol

Parameters
NameType
specimenany
typename"symbol"
optDetails?Details
Returns

asserts specimen is symbol

Defined in

types.js:121


AssertTypeofUndefined

Ƭ AssertTypeofUndefined: (specimen: any, typename: "undefined", optDetails?: Details) => asserts specimen is undefined

Type declaration

▸ (specimen, typename, optDetails?): asserts specimen is undefined

Parameters
NameType
specimenany
typename"undefined"
optDetails?Details
Returns

asserts specimen is undefined

Defined in

types.js:129


BaseAssert

Ƭ BaseAssert: (flag: any, optDetails?: Details, ErrorConstructor?: ErrorConstructor) => asserts flag

The assert function itself.

Type declaration

▸ (flag, optDetails?, ErrorConstructor?): asserts flag

Parameters
NameTypeDescription
flaganyThe truthy/falsy value
optDetails?DetailsThe details to throw
ErrorConstructor?ErrorConstructorAn optional alternate error constructor to use.
Returns

asserts flag

Defined in

types.js:11


Details

Ƭ Details: string | DetailsToken

Either a plain string, or made by the details template literal tag.

Defined in

types.js:178


DetailsTag

Ƭ DetailsTag: (template: TemplateStringsArray | string[], ...args: any) => DetailsToken

Use the details function as a template literal tag to create informative error messages. The assertion functions take such messages as optional arguments:

js
assert(sky.isBlue(), details`${sky.color} should be "blue"`);

or following the normal convention to locally rename details to X and quote to q like const { details: X, quote: q } = assert;:

js
assert(sky.isBlue(), X`${sky.color} should be "blue"`);

However, note that in most cases it is preferable to instead use the Fail template literal tag (which has the same input signature as details but automatically creates and throws an error):

js
sky.isBlue() || Fail`${sky.color} should be "blue"`;

The details template tag returns a DetailsToken object that can print itself with the formatted message in two ways. It will report full details to the console, but mask embedded substitution values with their typeof information in the thrown error to prevent revealing secrets up the exceptional path. In the example above, the thrown error may reveal only that sky.color is a string, whereas the same diagnostic printed to the console reveals that the sky was green. This masking can be disabled for an individual substitution value using quote.

The raw property of an input template array is ignored, so a simple array of strings may be provided directly.

Type declaration

▸ (template, ...args): DetailsToken

Parameters
NameType
templateTemplateStringsArray | string[]
...argsany
Returns

DetailsToken

Defined in

types.js:250


DetailsToken

Ƭ DetailsToken: Object

A call to the details template literal makes and returns a fresh details token, which is a frozen empty object associated with the arguments of that details template literal expression.

Defined in

types.js:171


FailTag

Ƭ FailTag: (template: TemplateStringsArray | string[], ...args: any) => never

Use the Fail function as a template literal tag to efficiently create and throw a details-style error only when a condition is not satisfied.

js
condition || Fail`...complaint...`;

This avoids the overhead of creating usually-unnecessary errors like

js
assert(condition, details`...complaint...`);

while improving readability over alternatives like

js
condition || assert.fail(details`...complaint...`);

However, due to current weakness in TypeScript, static reasoning is less powerful with the || patterns than with an assert call. Until/unless https://github.com/microsoft/TypeScript/issues/51426 is fixed, for ||-style assertions where this loss of static reasoning is a problem, instead express the assertion as

js
  if (!condition) {
    Fail`...complaint...`;
  }

or, if needed,

js
  if (!condition) {
    // `throw` is noop since `Fail` throws, but it improves static analysis
    throw Fail`...complaint...`;
  }

Type declaration

▸ (template, ...args): never

Parameters
NameType
templateTemplateStringsArray | string[]
...argsany
Returns

never

Defined in

types.js:285


MakeAssert

Ƭ MakeAssert: (optRaise?: Raise, unredacted?: boolean) => Assert

Makes and returns an assert function object that shares the bookkeeping state defined by this module with other assert function objects made by makeAssert. This state is per-module-instance and is exposed by the loggedErrorHandler above. We refer to assert as a "function object" because it can be called directly as a function, but also has methods that can be called.

If optRaise is provided, the returned assert function object will call optRaise(reason) before throwing the error. This enables optRaise to engage in even more violent termination behavior, like terminating the vat, that prevents execution from reaching the following throw. However, if optRaise returns normally, which would be unusual, the throw following optRaise(reason) would still happen.

Type declaration

▸ (optRaise?, unredacted?): Assert

Parameters
NameType
optRaise?Raise
unredacted?boolean
Returns

Assert

Defined in

types.js:228


Raise

Ƭ Raise: (reason: Error) => any

To make an assert which terminates some larger unit of computation like a transaction, vat, or process, call makeAssert with a Raise callback, where that callback actually performs that larger termination. If possible, the callback should also report its reason parameter as the alleged reason for the termination.

Type declaration

▸ (reason): any

Parameters
NameType
reasonError
Returns

any

Defined in

types.js:216