Skip to content

Commit

Permalink
fix: use assert rather than FooError constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Feb 13, 2021
1 parent 6e05fd4 commit f860c5b
Showing 143 changed files with 934 additions and 881 deletions.
5 changes: 4 additions & 1 deletion packages/ERTP/test/swingsetTests/splitPayments/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { E } from '@agoric/eventual-send';
import { makeIssuerKit } from '../../../src';

const { details: X } = assert;

export function buildRootObject(vatPowers, vatParameters) {
function testSplitPayments(aliceMaker) {
vatPowers.testLog('start test splitPayments');
@@ -12,14 +14,15 @@ export function buildRootObject(vatPowers, vatParameters) {
}

const obj0 = {
// eslint-disable-next-line consistent-return
async bootstrap(vats) {
switch (vatParameters.argv[0]) {
case 'splitPayments': {
const aliceMaker = await E(vats.alice).makeAliceMaker();
return testSplitPayments(aliceMaker);
}
default: {
throw Error(`unrecognized argument value ${vatParameters.argv[0]}`);
assert.fail(X`unrecognized argument value ${vatParameters.argv[0]}`);
}
}
},
6 changes: 2 additions & 4 deletions packages/SwingSet/src/assertOptions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { assert } from '@agoric/assert';
import { assert, details as X } from '@agoric/assert';

export function assertKnownOptions(options, knownNames) {
assert(knownNames instanceof Array);
for (const name of Object.keys(options)) {
if (knownNames.indexOf(name) === -1) {
throw Error(`unknown option ${name}`);
}
assert(knownNames.indexOf(name) !== -1, X`unknown option ${name}`);
}
}
4 changes: 2 additions & 2 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import * as babelParser from '@agoric/babel-parser';
import babelGenerate from '@babel/generator';
import anylogger from 'anylogger';

import { assert } from '@agoric/assert';
import { assert, details as X } from '@agoric/assert';
import { isTamed, tameMetering } from '@agoric/tame-metering';
import { importBundle } from '@agoric/import-bundle';
import { initSwingStore } from '@agoric/swing-store-simple';
@@ -83,7 +83,7 @@ export async function makeSwingsetController(
// sure vats get (and stick with) re2 for their 'RegExp'.
return re2;
} else {
throw Error(`kernelRequire unprepared to satisfy require(${what})`);
assert.fail(X`kernelRequire unprepared to satisfy require(${what})`);
}
}
const kernelBundle = JSON.parse(hostStorage.get('kernelBundle'));
6 changes: 3 additions & 3 deletions packages/SwingSet/src/devices/bridge-src.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { details: X } = assert;

function sanitize(data) {
// TODO: Use @agoric/marshal:pureCopy when it exists.
if (data === undefined) {
@@ -15,9 +17,7 @@ export function buildRootDeviceNode(tools) {
let { inboundHandler } = getDeviceState() || {};

function inboundCallback(...args) {
if (!inboundHandler) {
throw new Error(`inboundHandler not yet registered`);
}
assert(inboundHandler, X`inboundHandler not yet registered`);
const safeArgs = JSON.parse(JSON.stringify(args));
try {
SO(inboundHandler).inbound(...harden(safeArgs));
4 changes: 3 additions & 1 deletion packages/SwingSet/src/devices/command-src.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Nat from '@agoric/nat';

const { details: X } = assert;

export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
const {
@@ -20,7 +22,7 @@ export function buildRootDeviceNode(tools) {
SO(inboundHandler).inbound(Nat(count), body);
} catch (e) {
console.error(`error during inboundCallback:`, e);
throw new Error(`error during inboundCallback: ${e}`);
assert.fail(X`error during inboundCallback: ${e}`);
}
});

16 changes: 6 additions & 10 deletions packages/SwingSet/src/devices/command.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Nat from '@agoric/nat';
import { makePromiseKit } from '@agoric/promise-kit';

const { details: X } = assert;

export default function buildCommand(broadcastCallback) {
if (!broadcastCallback) {
throw new Error(`broadcastCallback must be provided.`);
}
assert(broadcastCallback, X`broadcastCallback must be provided.`);
let inboundCallback;
const srcPath = require.resolve('./command-src');
let nextCount = 0;
@@ -18,9 +18,7 @@ export default function buildCommand(broadcastCallback) {
const count = nextCount;
nextCount += 1;
responses.set(count, { resolve, reject });
if (!inboundCallback) {
throw new Error(`inboundCommand before registerInboundCallback`);
}
assert(inboundCallback, X`inboundCommand before registerInboundCallback`);
try {
inboundCallback(count, JSON.stringify(obj));
} catch (e) {
@@ -35,9 +33,7 @@ export default function buildCommand(broadcastCallback) {
}

function registerInboundCallback(cb) {
if (inboundCallback) {
throw new Error(`registerInboundCallback called more than once`);
}
assert(!inboundCallback, X`registerInboundCallback called more than once`);
inboundCallback = cb;
}

@@ -52,7 +48,7 @@ export default function buildCommand(broadcastCallback) {
}
if (!responses.has(count)) {
// maybe just ignore it
throw new Error(`unknown response index ${count}`);
assert.fail(X`unknown response index ${count}`);
}
const { resolve, reject } = responses.get(count);
if (isReject) {
10 changes: 4 additions & 6 deletions packages/SwingSet/src/devices/loopbox-src.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { details: X } = assert;

export function buildRootDeviceNode(tools) {
const { SO, endowments } = tools;
const { registerPassOneMessage, deliverMode } = endowments;
@@ -17,19 +19,15 @@ export function buildRootDeviceNode(tools) {

return harden({
registerInboundHandler(name, handler) {
if (inboundHandlers.has(name)) {
throw new Error(`already registered`);
}
assert(!inboundHandlers.has(name), X`already registered`);
inboundHandlers.set(name, handler);
},

makeSender(sender) {
let count = 1;
return harden({
add(peer, msgnum, body) {
if (!inboundHandlers.has(peer)) {
throw new Error(`unregistered peer '${peer}'`);
}
assert(inboundHandlers.has(peer), X`unregistered peer '${peer}'`);
const h = inboundHandlers.get(peer);
if (deliverMode === 'immediate') {
SO(h).deliverInboundMessages(sender, harden([[count, body]]));
9 changes: 6 additions & 3 deletions packages/SwingSet/src/devices/loopbox.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { details: X } = assert;

/*
* The "loopbox" is a special device used for unit tests, which glues one
* comms+vattp pair to another, within the same swingset machine. It looks
@@ -15,9 +17,10 @@
*/

export function buildLoopbox(deliverMode) {
if (deliverMode !== 'immediate' && deliverMode !== 'queued') {
throw Error(`deliverMode=${deliverMode}, must be 'immediate' or 'queued'`);
}
assert(
deliverMode === 'immediate' || deliverMode === 'queued',
X`deliverMode=${deliverMode}, must be 'immediate' or 'queued'`,
);
const loopboxSrcPath = require.resolve('./loopbox-src');

let loopboxPassOneMessage;
23 changes: 11 additions & 12 deletions packages/SwingSet/src/devices/mailbox-src.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Nat from '@agoric/nat';

const { details: X } = assert;

export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
const highestInboundDelivered = harden(new Map());
@@ -55,9 +57,10 @@ export function buildRootDeviceNode(tools) {

// console.debug(`mailbox-src build: inboundHandler is`, inboundHandler);
deliverInboundMessages = (peer, newMessages) => {
if (!inboundHandler) {
throw new Error(`deliverInboundMessages before registerInboundHandler`);
}
assert(
inboundHandler,
X`deliverInboundMessages before registerInboundHandler`,
);
try {
SO(inboundHandler).deliverInboundMessages(peer, newMessages);
} catch (e) {
@@ -66,9 +69,7 @@ export function buildRootDeviceNode(tools) {
};

deliverInboundAck = (peer, ack) => {
if (!inboundHandler) {
throw new Error(`deliverInboundAck before registerInboundHandler`);
}
assert(inboundHandler, X`deliverInboundAck before registerInboundHandler`);
try {
SO(inboundHandler).deliverInboundAck(peer, ack);
} catch (e) {
@@ -79,9 +80,7 @@ export function buildRootDeviceNode(tools) {
// the Root Device Node.
return harden({
registerInboundHandler(handler) {
if (inboundHandler) {
throw new Error(`already registered`);
}
assert(!inboundHandler, X`already registered`);
inboundHandler = handler;
setDeviceState(harden({ inboundHandler }));
},
@@ -90,23 +89,23 @@ export function buildRootDeviceNode(tools) {
try {
endowments.add(`${peer}`, Nat(msgnum), `${body}`);
} catch (e) {
throw new Error(`error in add: ${e}`);
assert.fail(X`error in add: ${e}`);
}
},

remove(peer, msgnum) {
try {
endowments.remove(`${peer}`, Nat(msgnum));
} catch (e) {
throw new Error(`error in remove: ${e}`);
assert.fail(X`error in remove: ${e}`);
}
},

ackInbound(peer, msgnum) {
try {
endowments.setAcknum(`${peer}`, Nat(msgnum));
} catch (e) {
throw new Error(`error in ackInbound: ${e}`);
assert.fail(X`error in ackInbound: ${e}`);
}
},
});
14 changes: 7 additions & 7 deletions packages/SwingSet/src/devices/mailbox.js
Original file line number Diff line number Diff line change
@@ -66,6 +66,8 @@

import Nat from '@agoric/nat';

const { details: X } = assert;

// This Map-based mailboxState object is a good starting point, but we may
// replace it with one that tracks which parts of the state have been
// modified, to build more efficient Merkle proofs.
@@ -130,16 +132,14 @@ export function buildMailboxStateMap(state = harden(new Map())) {
}

function populateFromData(data) {
if (state.size) {
throw new Error(`cannot populateFromData: outbox is not empty`);
}
assert(!state.size, X`cannot populateFromData: outbox is not empty`);
for (const peer of Object.getOwnPropertyNames(data)) {
const inout = getOrCreatePeer(peer);
const d = data[peer];
const dp = data[peer];
importMailbox(
{
ack: d.inboundAck,
outbox: d.outbox,
ack: dp.inboundAck,
outbox: dp.outbox,
},
inout,
);
@@ -185,7 +185,7 @@ export function buildMailbox(state) {
try {
return Boolean(inboundCallback(peer, messages, ack));
} catch (e) {
throw new Error(`error in inboundCallback: ${e}`);
assert.fail(X`error in inboundCallback: ${e}`);
}
}

10 changes: 4 additions & 6 deletions packages/SwingSet/src/devices/plugin-src.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

import { makeCapTP } from '@agoric/captp';

const { details: X } = assert;

export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
const restart = getDeviceState();
@@ -95,9 +97,7 @@ export function buildRootDeviceNode(tools) {
function send(index, obj) {
const mod = connectedMods[index];
// console.info('send', index, obj, mod);
if (!mod) {
throw TypeError(`No module associated with ${index}`);
}
assert(mod, X`No module associated with ${index}`, TypeError);
let sender = senders[index];
if (!sender) {
// Lazily create a fresh sender.
@@ -124,9 +124,7 @@ export function buildRootDeviceNode(tools) {
connect,
send,
registerReceiver(receiver) {
if (registeredReceiver) {
throw Error(`registered receiver already set`);
}
assert(!registeredReceiver, X`registered receiver already set`);
registeredReceiver = receiver;
saveState();
},
4 changes: 3 additions & 1 deletion packages/SwingSet/src/devices/timer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Nat from '@agoric/nat';

const { details: X } = assert;

/**
* Endowments for a Timer device that can be made available to SwingSet vats.
*
@@ -23,7 +25,7 @@ export function buildTimer() {
try {
return Boolean(devicePollFunction(Nat(time)));
} catch (e) {
throw new Error(`error in devicePollFunction: ${e}`);
assert.fail(X`error in devicePollFunction: ${e}`);
}
}

16 changes: 6 additions & 10 deletions packages/SwingSet/src/hostStorage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { initSwingStore } from '@agoric/swing-store-simple';

const { details: X } = assert;

/*
The "Storage API" is a set of functions { has, getKeys, get, set, delete } that
work on string keys and accept string values. A lot of kernel-side code
@@ -97,21 +99,15 @@ export function buildHostDBInMemory(storage) {
// after earlier changes have actually been applied, potentially leaving
// the store in an indeterminate state. Problem? I suspect so...
for (const c of changes) {
if (`${c.op}` !== c.op) {
throw new Error(`non-string c.op ${c.op}`);
}
if (`${c.key}` !== c.key) {
throw new Error(`non-string c.key ${c.key}`);
}
assert(`${c.op}` === c.op, X`non-string c.op ${c.op}`);
assert(`${c.key}` === c.key, X`non-string c.key ${c.key}`);
if (c.op === 'set') {
if (`${c.value}` !== c.value) {
throw new Error(`non-string c.value ${c.value}`);
}
assert(`${c.value}` === c.value, X`non-string c.value ${c.value}`);
storage.set(c.key, c.value);
} else if (c.op === 'delete') {
storage.delete(c.key);
} else {
throw new Error(`unknown c.op ${c.op}`);
assert.fail(X`unknown c.op ${c.op}`);
}
}
}
Loading

0 comments on commit f860c5b

Please sign in to comment.