Skip to content

Commit

Permalink
support our assert conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jan 26, 2024
1 parent 5b159cb commit a0ea3b4
Show file tree
Hide file tree
Showing 76 changed files with 118 additions and 116 deletions.
1 change: 1 addition & 0 deletions packages/captp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"c8": "^7.14.0"
},
"dependencies": {
"@endo/errors": "^1.0.2",
"@endo/eventual-send": "^1.1.0",
"@endo/marshal": "^1.1.0",
"@endo/nat": "^5.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/captp/src/atomics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="ses"/>

const { details: X, Fail } = assert;
import { X, Fail } from '@endo/errors';

// This is a pathological minimum, but exercised by the unit test.
export const MIN_DATA_BUFFER_LENGTH = 1;
Expand Down Expand Up @@ -154,7 +154,7 @@ export const makeAtomicsTrapGuest = transferBuffer => {
//
// TODO: It would be nice to use an error type, but captp is just too
// noisy with spurious "Temporary logging of sent error" messages.
// it.throw(assert.error(X`Trap host has not finished`));
// it.throw(makeError(X`Trap host has not finished`));
it.throw(null);

// eslint-disable-next-line no-bitwise
Expand Down
5 changes: 2 additions & 3 deletions packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import { Remotable, Far, makeMarshal, QCLASS } from '@endo/marshal';
import { E, HandledPromise } from '@endo/eventual-send';
import { isPromise, makePromiseKit } from '@endo/promise-kit';

import { X, Fail, errorNote } from '@endo/errors';
import { makeTrap } from './trap.js';

import { makeFinalizingMap } from './finalize.js';

export { E };

const { details: X, Fail } = assert;

const WELL_KNOWN_SLOT_PROPERTIES = harden(['answerID', 'questionID', 'target']);

/**
Expand Down Expand Up @@ -670,7 +669,7 @@ export const makeCapTP = (
if (!e) {
Fail`trapGuest expected trapHost AsyncIterator(${questionID}) to be done, but it wasn't`;
}
assert.note(e, X`trapHost AsyncIterator(${questionID}) threw`);
errorNote(e, X`trapHost AsyncIterator(${questionID}) threw`);
throw e;
}
};
Expand Down
3 changes: 1 addition & 2 deletions packages/captp/test/traplib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
/// <reference types="ses"/>

import { Far } from '@endo/marshal';
import { X, Fail } from '@endo/errors';
import { E, makeCapTP } from '../src/captp.js';

import { makeAtomicsTrapGuest, makeAtomicsTrapHost } from '../src/atomics.js';

const { details: X, Fail } = assert;

export const createHostBootstrap = makeTrapHandler => {
// Create a remotable that has a syncable return value.
return Far('test traps', {
Expand Down
3 changes: 1 addition & 2 deletions packages/captp/test/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import '@endo/init/pre-remoting.js';
import '@endo/init/debug.js';

import { parentPort } from 'worker_threads';
import { Fail } from '@endo/errors';
import { makeGuest, makeHost } from './traplib.js';

const { Fail } = assert;

let dispatch;
parentPort.addListener('message', obj => {
switch (obj.type) {
Expand Down
10 changes: 5 additions & 5 deletions packages/check-bundle/lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { decodeBase64 } from '@endo/base64/decode.js';
import { parseArchive } from '@endo/compartment-mapper/import-archive.js';

const { Fail, details: d, quote: q } = assert;
import { Fail, X, q } from '@endo/errors';

/**
* Verifies that a bundle passes its own integrity checks or rejects the
Expand All @@ -25,7 +25,7 @@ export const checkBundle = async (
assert.typeof(
bundle,
'object',
d`checkBundle cannot hash non-bundle, must be of type object, got ${q(
X`checkBundle cannot hash non-bundle, must be of type object, got ${q(
bundle,
)}`,
);
Expand Down Expand Up @@ -56,20 +56,20 @@ export const checkBundle = async (
assert.typeof(
moduleFormat,
'string',
d`checkBundle cannot hash non-bundle, moduleFormat must be a string, got ${typeof moduleFormat}`,
X`checkBundle cannot hash non-bundle, moduleFormat must be a string, got ${typeof moduleFormat}`,
);

if (moduleFormat === 'endoZipBase64') {
const { endoZipBase64, endoZipBase64Sha512 } = bundle;
assert.typeof(
endoZipBase64,
'string',
d`checkBundle cannot hash non-bundle, property 'endoZipBase64' must be a string, got ${typeof endoZipBase64}`,
X`checkBundle cannot hash non-bundle, property 'endoZipBase64' must be a string, got ${typeof endoZipBase64}`,
);
assert.typeof(
endoZipBase64Sha512,
'string',
d`checkBundle cannot bundle without the property 'endoZipBase64Sha512', which must be a string, got ${typeof endoZipBase64Sha512}`,
X`checkBundle cannot bundle without the property 'endoZipBase64Sha512', which must be a string, got ${typeof endoZipBase64Sha512}`,
);
const bytes = decodeBase64(endoZipBase64);
const { sha512: parsedSha512 } = await parseArchive(bytes, bundleName, {
Expand Down
3 changes: 2 additions & 1 deletion packages/check-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"dependencies": {
"@endo/base64": "^1.0.1",
"@endo/compartment-mapper": "^1.1.0"
"@endo/compartment-mapper": "^1.1.0",
"@endo/errors": "^1.0.2"
},
"devDependencies": {
"@endo/bundle-source": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/common/from-unique-entries.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { q, Fail } from '@endo/errors';

const { fromEntries } = Object;
const { ownKeys } = Reflect;

const { quote: q, Fail } = assert;

/**
* Throws if multiple entries use the same property name. Otherwise acts
* like `Object.fromEntries` but hardens the result.
Expand Down
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"test:xs": "exit 0"
},
"dependencies": {
"@endo/errors": "^1.0.2",
"@endo/eventual-send": "^1.1.0",
"@endo/promise-kit": "^1.0.2"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/common/test/test-apply-labeling-error.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Fail } from '@endo/errors';
import { test } from './prepare-test-env-ava.js';
import { applyLabelingError } from '../apply-labeling-error.js';

const { Fail } = assert;

test('test applyLabelingError', async t => {
t.is(
applyLabelingError(x => x * 2, [8]),
Expand Down
9 changes: 3 additions & 6 deletions packages/common/throw-labeled.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { details: X } = assert;
import { X, makeError, errorNote } from '@endo/errors';

/**
* Given an error `innerErr` and a `label`, throws a similar
Expand All @@ -14,11 +14,8 @@ export const throwLabeled = (innerErr, label, ErrorConstructor = undefined) => {
if (typeof label === 'number') {
label = `[${label}]`;
}
const outerErr = assert.error(
`${label}: ${innerErr.message}`,
ErrorConstructor,
);
assert.note(outerErr, X`Caused by ${innerErr}`);
const outerErr = makeError(`${label}: ${innerErr.message}`, ErrorConstructor);
errorNote(outerErr, X`Caused by ${innerErr}`);
throw outerErr;
};
harden(throwLabeled);
1 change: 1 addition & 0 deletions packages/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"dependencies": {
"@endo/captp": "^4.0.2",
"@endo/errors": "^1.0.2",
"@endo/eventual-send": "^1.1.0",
"@endo/far": "^1.0.2",
"@endo/lockdown": "^1.0.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import url from 'url';

import { E, Far } from '@endo/far';
import { makePromiseKit } from '@endo/promise-kit';
import { q } from '@endo/errors';
import { makeNodeNetstringCapTP } from './connection.js';

const { quote: q } = assert;

const { promise: cancelled, reject: cancel } = makePromiseKit();

// TODO thread through command arguments.
Expand Down
6 changes: 6 additions & 0 deletions packages/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ export {
quote,
redacted,
throwRedacted,
// conventional abbreviations and aliases
bare as b,
quote as q,
redacted as X,
throwRedacted as Fail,
note as errorNote,
};
1 change: 1 addition & 0 deletions packages/eventual-send/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"homepage": "https://github.com/endojs/endo#readme",
"dependencies": {
"@endo/errors": "^1.0.2",
"@endo/env-options": "^1.1.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/eventual-send/src/E.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { X, q, Fail, makeError } from '@endo/errors';
import { trackTurns } from './track-turns.js';
import { makeMessageBreakpointTester } from './message-breakpoints.js';

const { details: X, quote: q, Fail } = assert;
const { assign, create } = Object;

const onSend = makeMessageBreakpointTester('ENDO_SEND_BREAKPOINTS');
Expand Down Expand Up @@ -52,8 +52,8 @@ const makeEProxyHandler = (recipient, HandledPromise) =>
if (this !== receiver) {
// Reject the async function call
return HandledPromise.reject(
assert.error(
X`Unexpected receiver for "${propertyKey}" method of E(${q(
makeError(
X`Unexpected receiver for "${q(propertyKey)}" method of E(${q(
recipient,
)})`,
),
Expand Down
5 changes: 2 additions & 3 deletions packages/eventual-send/src/handled-promise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="ses" />

import { Fail, X, q, errorNote } from '@endo/errors';
import { trackTurns } from './track-turns.js';
import {
localApplyFunction,
Expand All @@ -9,8 +10,6 @@ import {
} from './local.js';
import { makePostponedHandler } from './postponed.js';

const { Fail, details: X, quote: q } = assert;

const {
create,
freeze,
Expand Down Expand Up @@ -337,7 +336,7 @@ export const makeHandledPromise = () => {
handledResolve(resolvedTarget);
return resolvedTarget;
} catch (e) {
assert.note(e, X`during resolveWithPresence`);
errorNote(e, X`during resolveWithPresence`);
handledReject(e);
throw e;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/eventual-send/src/local.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { X, q, Fail } from '@endo/errors';
import { makeMessageBreakpointTester } from './message-breakpoints.js';

const { details: X, quote: q, Fail } = assert;

const { getOwnPropertyDescriptors, getPrototypeOf, freeze } = Object;
const { apply, ownKeys } = Reflect;

Expand Down
2 changes: 1 addition & 1 deletion packages/eventual-send/src/message-breakpoints.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getEnvironmentOption } from '@endo/env-options';

const { quote: q, Fail } = assert;
import { q, Fail } from '@endo/errors';

const { hasOwn, freeze, entries } = Object;

Expand Down
14 changes: 7 additions & 7 deletions packages/eventual-send/src/track-turns.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global globalThis */
import { X, errorNote } from '@endo/errors';
import {
getEnvironmentOption,
environmentOptionsListHas,
} from '@endo/env-options';

// NOTE: We can't import these because they're not in scope before lockdown.
// import { assert, details as X, Fail } from '@agoric/assert';
// import { assert, X, Fail } from '@agoric/assert';

// WARNING: Global Mutable State!
// This state is communicated to `assert` that makes it available to the
Expand Down Expand Up @@ -33,15 +34,15 @@ const ENABLED =

const addRejectionNote = detailsNote => reason => {
if (reason instanceof Error) {
assert.note(reason, detailsNote);
errorNote(reason, detailsNote);
}
if (VERBOSE) {
console.log('REJECTED at top of event loop', reason);
}
};

const wrapFunction =
(func, sendingError, X) =>
(func, sendingError) =>
(...args) => {
hiddenPriorError = sendingError;
hiddenCurrentTurn += 1;
Expand All @@ -52,7 +53,7 @@ const wrapFunction =
result = func(...args);
} catch (err) {
if (err instanceof Error) {
assert.note(
errorNote(
err,
X`Thrown from: ${hiddenPriorError}:${hiddenCurrentTurn}.${hiddenCurrentEvent}`,
);
Expand Down Expand Up @@ -90,18 +91,17 @@ export const trackTurns = funcs => {
if (!ENABLED || typeof globalThis === 'undefined' || !globalThis.assert) {
return funcs;
}
const { details: X } = assert;

hiddenCurrentEvent += 1;
const sendingError = Error(
`Event: ${hiddenCurrentTurn}.${hiddenCurrentEvent}`,
);
if (hiddenPriorError !== undefined) {
assert.note(sendingError, X`Caused by: ${hiddenPriorError}`);
errorNote(sendingError, X`Caused by: ${hiddenPriorError}`);
}

return /** @type {T} */ (
funcs.map(func => func && wrapFunction(func, sendingError, X))
funcs.map(func => func && wrapFunction(func, sendingError))
);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/eventual-send/test/test-eventual-send.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* global setTimeout */
import { X } from '@endo/errors';
import { test } from './prepare-test-env-ava.js';

import { HandledPromise } from './get-hp.js';

const { getPrototypeOf } = Object;
const { details: X } = assert;

if (typeof window !== 'undefined') {
// Let the browser detect when the tests are done.
Expand Down
3 changes: 1 addition & 2 deletions packages/eventual-send/test/test-hp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { q } from '@endo/errors';
import { test } from './prepare-test-env-ava.js';

import { HandledPromise } from './get-hp.js';

const { getPrototypeOf, isFrozen } = Object;
const { ownKeys, getOwnPropertyDescriptor } = Reflect;

const { quote: q } = assert;

test('sufficiently hardened', t => {
const expectedReachableIntrinsics = new Set([
Function.prototype,
Expand Down
1 change: 1 addition & 0 deletions packages/exo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@endo/common": "^1.0.2",
"@endo/errors": "^1.0.2",
"@endo/env-options": "^1.1.0",
"@endo/eventual-send": "^1.1.0",
"@endo/far": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/exo/src/exo-makers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { environmentOptionsListHas } from '@endo/env-options';
import { objectMap } from '@endo/common/object-map.js';

import { Fail, q } from '@endo/errors';
import { defendPrototype, defendPrototypeKit } from './exo-tools.js';

const { Fail, quote: q } = assert;
const { create, seal, freeze, defineProperty, values } = Object;

// Turn on to give each exo instance its own toStringTag value.
Expand Down
Loading

0 comments on commit a0ea3b4

Please sign in to comment.