Skip to content

Commit

Permalink
refactor(errors): 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 5ac4679
Show file tree
Hide file tree
Showing 69 changed files with 111 additions and 101 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
5 changes: 3 additions & 2 deletions packages/common/test/test-apply-labeling-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { test } from './prepare-test-env-ava.js';
import { applyLabelingError } from '../apply-labeling-error.js';

const { Fail } = assert;
// eslint-disable-next-line import/order
import { Fail } from '@endo/errors';
import { applyLabelingError } from '../apply-labeling-error.js';

test('test applyLabelingError', async t => {
t.is(
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,
};
2 changes: 1 addition & 1 deletion packages/eventual-send/src/E.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const makeEProxyHandler = (recipient, HandledPromise) =>
// Reject the async function call
return HandledPromise.reject(
assert.error(
X`Unexpected receiver for "${propertyKey}" method of E(${q(
X`Unexpected receiver for "${q(propertyKey)}" method of E(${q(
recipient,
)})`,
),
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
2 changes: 1 addition & 1 deletion packages/exo/src/exo-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@endo/patterns';
import { listDifference } from '@endo/common/list-difference.js';
import { objectMap } from '@endo/common/object-map.js';
import { q, Fail } from '@endo/errors';
import { GET_INTERFACE_GUARD } from './get-interface.js';

/** @typedef {import('@endo/patterns').Method} Method */
Expand All @@ -23,7 +24,6 @@ import { GET_INTERFACE_GUARD } from './get-interface.js';
* @typedef {import('@endo/patterns').InterfaceGuard<T>} InterfaceGuard
*/

const { quote: q, Fail } = assert;
const { apply, ownKeys } = Reflect;
const { defineProperties, fromEntries } = Object;

Expand Down
5 changes: 3 additions & 2 deletions packages/exo/test/test-exo-wobbly-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import { test } from './prepare-test-env-ava.js';
import { getMethodNames } from '@endo/eventual-send/utils.js';
import { passStyleOf, Far, GET_METHOD_NAMES } from '@endo/pass-style';
import { M } from '@endo/patterns';
import { defineExoClass } from '../src/exo-makers.js';

import { Fail, q } from '@endo/errors';
import { GET_INTERFACE_GUARD } from '../src/get-interface.js';
import { defineExoClass } from '../src/exo-makers.js';

const { Fail, quote: q } = assert;
const { apply } = Reflect;

const ExoEmptyI = M.interface('ExoEmpty', {});
Expand Down
4 changes: 2 additions & 2 deletions packages/exo/test/test-label-instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { test } from './prepare-test-env-ava-label-instances.js';
// eslint-disable-next-line import/order
import { passStyleOf } from '@endo/far';
import { M } from '@endo/patterns';

import { q } from '@endo/errors';
import {
defineExoClass,
defineExoClassKit,
makeExo,
} from '../src/exo-makers.js';

const { quote: q } = assert;

const UpCounterI = M.interface('UpCounter', {
incr: M.call().returns(M.number()),
});
Expand Down
1 change: 1 addition & 0 deletions packages/far/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/endojs/endo#readme",
"dependencies": {
"@endo/errors": "^1.0.2",
"@endo/eventual-send": "^1.1.0",
"@endo/pass-style": "^1.1.0"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/far/test/test-marshal-far-obj.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// @ts-check

import { test } from './prepare-test-env-ava.js';

// eslint-disable-next-line import/order
import { q } from '@endo/errors';

import { Far, passStyleOf, getInterfaceOf } from '../src/index.js';

const { quote: q } = assert;
const { create, getPrototypeOf } = Object;

// this only includes the tests that do not use liveSlots
Expand Down
1 change: 1 addition & 0 deletions packages/import-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@endo/base64": "^1.0.1",
"@endo/compartment-mapper": "^1.1.0",
"@endo/errors": "^1.0.2",
"@endo/where": "^1.0.1",
"ses": "^1.1.0"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/import-bundle/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import { parseArchive } from '@endo/compartment-mapper/import-archive.js';
import { decodeBase64 } from '@endo/base64';
import { Fail } from '@endo/errors';
import { wrapInescapableCompartment } from './compartment-wrapper.js';

const { Fail } = assert;

// importBundle takes the output of bundle-source, and returns a namespace
// object (with .default, and maybe other properties for named exports)

Expand Down
1 change: 1 addition & 0 deletions packages/lp32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"test": "ava"
},
"dependencies": {
"@endo/errors": "^1.0.2",
"@endo/init": "^1.0.2",
"@endo/stream": "^1.0.2",
"ses": "^1.1.0"
Expand Down
3 changes: 1 addition & 2 deletions packages/lp32/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// We use a DataView to give users choice over endianness.
// But DataView does not default to host-byte-order like other typed arrays.

import { Fail, q } from '@endo/errors';
import { hostIsLittleEndian } from './src/host-endian.js';

const { Fail, quote: q } = assert;

/**
* @param {Iterable<Uint8Array> | AsyncIterable<Uint8Array>} reader
* @param {object} opts
Expand Down
3 changes: 1 addition & 2 deletions packages/lp32/writer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// @ts-check
/// <reference types="ses"/>

import { Fail, q } from '@endo/errors';
import { hostIsLittleEndian } from './src/host-endian.js';

const { Fail, quote: q } = assert;

/**
* @param {import('@endo/stream').Writer<Uint8Array, undefined>} output
* @param {object} opts
Expand Down
1 change: 1 addition & 0 deletions packages/marshal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"homepage": "https://github.com/endojs/endo#readme",
"dependencies": {
"@endo/errors": "^1.0.2",
"@endo/eventual-send": "^1.1.0",
"@endo/nat": "^5.0.2",
"@endo/pass-style": "^1.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/marshal/src/deeplyFulfilled.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { getTag, isObject, makeTagged, passStyleOf } from '@endo/pass-style';
/** @typedef {import('@endo/pass-style').Passable} Passable */
/** @template T @typedef {import('@endo/eventual-send').ERef<T>} ERef */

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

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

Expand Down
2 changes: 1 addition & 1 deletion packages/marshal/src/dot-membrane.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import { E } from '@endo/eventual-send';
import { isObject, getInterfaceOf, Far, passStyleOf } from '@endo/pass-style';
import { Fail } from '@endo/errors';
import { makeMarshal } from './marshal.js';

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

// TODO(erights): Add Converter type
/** @param {any} [mirrorConverter] */
Expand Down
3 changes: 2 additions & 1 deletion packages/marshal/src/encodePassable.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
*/
/** @typedef {import('./types.js').RankCover} RankCover */

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

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

Expand Down
Loading

0 comments on commit 5ac4679

Please sign in to comment.