Skip to content

Commit f860c5b

Browse files
committed
fix: use assert rather than FooError constructors
1 parent 6e05fd4 commit f860c5b

File tree

143 files changed

+934
-881
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+934
-881
lines changed

packages/ERTP/test/swingsetTests/splitPayments/bootstrap.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { E } from '@agoric/eventual-send';
22
import { makeIssuerKit } from '../../../src';
33

4+
const { details: X } = assert;
5+
46
export function buildRootObject(vatPowers, vatParameters) {
57
function testSplitPayments(aliceMaker) {
68
vatPowers.testLog('start test splitPayments');
@@ -12,14 +14,15 @@ export function buildRootObject(vatPowers, vatParameters) {
1214
}
1315

1416
const obj0 = {
17+
// eslint-disable-next-line consistent-return
1518
async bootstrap(vats) {
1619
switch (vatParameters.argv[0]) {
1720
case 'splitPayments': {
1821
const aliceMaker = await E(vats.alice).makeAliceMaker();
1922
return testSplitPayments(aliceMaker);
2023
}
2124
default: {
22-
throw Error(`unrecognized argument value ${vatParameters.argv[0]}`);
25+
assert.fail(X`unrecognized argument value ${vatParameters.argv[0]}`);
2326
}
2427
}
2528
},
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { assert } from '@agoric/assert';
1+
import { assert, details as X } from '@agoric/assert';
22

33
export function assertKnownOptions(options, knownNames) {
44
assert(knownNames instanceof Array);
55
for (const name of Object.keys(options)) {
6-
if (knownNames.indexOf(name) === -1) {
7-
throw Error(`unknown option ${name}`);
8-
}
6+
assert(knownNames.indexOf(name) !== -1, X`unknown option ${name}`);
97
}
108
}

packages/SwingSet/src/controller.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as babelParser from '@agoric/babel-parser';
99
import babelGenerate from '@babel/generator';
1010
import anylogger from 'anylogger';
1111

12-
import { assert } from '@agoric/assert';
12+
import { assert, details as X } from '@agoric/assert';
1313
import { isTamed, tameMetering } from '@agoric/tame-metering';
1414
import { importBundle } from '@agoric/import-bundle';
1515
import { initSwingStore } from '@agoric/swing-store-simple';
@@ -83,7 +83,7 @@ export async function makeSwingsetController(
8383
// sure vats get (and stick with) re2 for their 'RegExp'.
8484
return re2;
8585
} else {
86-
throw Error(`kernelRequire unprepared to satisfy require(${what})`);
86+
assert.fail(X`kernelRequire unprepared to satisfy require(${what})`);
8787
}
8888
}
8989
const kernelBundle = JSON.parse(hostStorage.get('kernelBundle'));

packages/SwingSet/src/devices/bridge-src.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { details: X } = assert;
2+
13
function sanitize(data) {
24
// TODO: Use @agoric/marshal:pureCopy when it exists.
35
if (data === undefined) {
@@ -15,9 +17,7 @@ export function buildRootDeviceNode(tools) {
1517
let { inboundHandler } = getDeviceState() || {};
1618

1719
function inboundCallback(...args) {
18-
if (!inboundHandler) {
19-
throw new Error(`inboundHandler not yet registered`);
20-
}
20+
assert(inboundHandler, X`inboundHandler not yet registered`);
2121
const safeArgs = JSON.parse(JSON.stringify(args));
2222
try {
2323
SO(inboundHandler).inbound(...harden(safeArgs));

packages/SwingSet/src/devices/command-src.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Nat from '@agoric/nat';
22

3+
const { details: X } = assert;
4+
35
export function buildRootDeviceNode(tools) {
46
const { SO, getDeviceState, setDeviceState, endowments } = tools;
57
const {
@@ -20,7 +22,7 @@ export function buildRootDeviceNode(tools) {
2022
SO(inboundHandler).inbound(Nat(count), body);
2123
} catch (e) {
2224
console.error(`error during inboundCallback:`, e);
23-
throw new Error(`error during inboundCallback: ${e}`);
25+
assert.fail(X`error during inboundCallback: ${e}`);
2426
}
2527
});
2628

packages/SwingSet/src/devices/command.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Nat from '@agoric/nat';
22
import { makePromiseKit } from '@agoric/promise-kit';
33

4+
const { details: X } = assert;
5+
46
export default function buildCommand(broadcastCallback) {
5-
if (!broadcastCallback) {
6-
throw new Error(`broadcastCallback must be provided.`);
7-
}
7+
assert(broadcastCallback, X`broadcastCallback must be provided.`);
88
let inboundCallback;
99
const srcPath = require.resolve('./command-src');
1010
let nextCount = 0;
@@ -18,9 +18,7 @@ export default function buildCommand(broadcastCallback) {
1818
const count = nextCount;
1919
nextCount += 1;
2020
responses.set(count, { resolve, reject });
21-
if (!inboundCallback) {
22-
throw new Error(`inboundCommand before registerInboundCallback`);
23-
}
21+
assert(inboundCallback, X`inboundCommand before registerInboundCallback`);
2422
try {
2523
inboundCallback(count, JSON.stringify(obj));
2624
} catch (e) {
@@ -35,9 +33,7 @@ export default function buildCommand(broadcastCallback) {
3533
}
3634

3735
function registerInboundCallback(cb) {
38-
if (inboundCallback) {
39-
throw new Error(`registerInboundCallback called more than once`);
40-
}
36+
assert(!inboundCallback, X`registerInboundCallback called more than once`);
4137
inboundCallback = cb;
4238
}
4339

@@ -52,7 +48,7 @@ export default function buildCommand(broadcastCallback) {
5248
}
5349
if (!responses.has(count)) {
5450
// maybe just ignore it
55-
throw new Error(`unknown response index ${count}`);
51+
assert.fail(X`unknown response index ${count}`);
5652
}
5753
const { resolve, reject } = responses.get(count);
5854
if (isReject) {

packages/SwingSet/src/devices/loopbox-src.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { details: X } = assert;
2+
13
export function buildRootDeviceNode(tools) {
24
const { SO, endowments } = tools;
35
const { registerPassOneMessage, deliverMode } = endowments;
@@ -17,19 +19,15 @@ export function buildRootDeviceNode(tools) {
1719

1820
return harden({
1921
registerInboundHandler(name, handler) {
20-
if (inboundHandlers.has(name)) {
21-
throw new Error(`already registered`);
22-
}
22+
assert(!inboundHandlers.has(name), X`already registered`);
2323
inboundHandlers.set(name, handler);
2424
},
2525

2626
makeSender(sender) {
2727
let count = 1;
2828
return harden({
2929
add(peer, msgnum, body) {
30-
if (!inboundHandlers.has(peer)) {
31-
throw new Error(`unregistered peer '${peer}'`);
32-
}
30+
assert(inboundHandlers.has(peer), X`unregistered peer '${peer}'`);
3331
const h = inboundHandlers.get(peer);
3432
if (deliverMode === 'immediate') {
3533
SO(h).deliverInboundMessages(sender, harden([[count, body]]));

packages/SwingSet/src/devices/loopbox.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { details: X } = assert;
2+
13
/*
24
* The "loopbox" is a special device used for unit tests, which glues one
35
* comms+vattp pair to another, within the same swingset machine. It looks
@@ -15,9 +17,10 @@
1517
*/
1618

1719
export function buildLoopbox(deliverMode) {
18-
if (deliverMode !== 'immediate' && deliverMode !== 'queued') {
19-
throw Error(`deliverMode=${deliverMode}, must be 'immediate' or 'queued'`);
20-
}
20+
assert(
21+
deliverMode === 'immediate' || deliverMode === 'queued',
22+
X`deliverMode=${deliverMode}, must be 'immediate' or 'queued'`,
23+
);
2124
const loopboxSrcPath = require.resolve('./loopbox-src');
2225

2326
let loopboxPassOneMessage;

packages/SwingSet/src/devices/mailbox-src.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Nat from '@agoric/nat';
22

3+
const { details: X } = assert;
4+
35
export function buildRootDeviceNode(tools) {
46
const { SO, getDeviceState, setDeviceState, endowments } = tools;
57
const highestInboundDelivered = harden(new Map());
@@ -55,9 +57,10 @@ export function buildRootDeviceNode(tools) {
5557

5658
// console.debug(`mailbox-src build: inboundHandler is`, inboundHandler);
5759
deliverInboundMessages = (peer, newMessages) => {
58-
if (!inboundHandler) {
59-
throw new Error(`deliverInboundMessages before registerInboundHandler`);
60-
}
60+
assert(
61+
inboundHandler,
62+
X`deliverInboundMessages before registerInboundHandler`,
63+
);
6164
try {
6265
SO(inboundHandler).deliverInboundMessages(peer, newMessages);
6366
} catch (e) {
@@ -66,9 +69,7 @@ export function buildRootDeviceNode(tools) {
6669
};
6770

6871
deliverInboundAck = (peer, ack) => {
69-
if (!inboundHandler) {
70-
throw new Error(`deliverInboundAck before registerInboundHandler`);
71-
}
72+
assert(inboundHandler, X`deliverInboundAck before registerInboundHandler`);
7273
try {
7374
SO(inboundHandler).deliverInboundAck(peer, ack);
7475
} catch (e) {
@@ -79,9 +80,7 @@ export function buildRootDeviceNode(tools) {
7980
// the Root Device Node.
8081
return harden({
8182
registerInboundHandler(handler) {
82-
if (inboundHandler) {
83-
throw new Error(`already registered`);
84-
}
83+
assert(!inboundHandler, X`already registered`);
8584
inboundHandler = handler;
8685
setDeviceState(harden({ inboundHandler }));
8786
},
@@ -90,23 +89,23 @@ export function buildRootDeviceNode(tools) {
9089
try {
9190
endowments.add(`${peer}`, Nat(msgnum), `${body}`);
9291
} catch (e) {
93-
throw new Error(`error in add: ${e}`);
92+
assert.fail(X`error in add: ${e}`);
9493
}
9594
},
9695

9796
remove(peer, msgnum) {
9897
try {
9998
endowments.remove(`${peer}`, Nat(msgnum));
10099
} catch (e) {
101-
throw new Error(`error in remove: ${e}`);
100+
assert.fail(X`error in remove: ${e}`);
102101
}
103102
},
104103

105104
ackInbound(peer, msgnum) {
106105
try {
107106
endowments.setAcknum(`${peer}`, Nat(msgnum));
108107
} catch (e) {
109-
throw new Error(`error in ackInbound: ${e}`);
108+
assert.fail(X`error in ackInbound: ${e}`);
110109
}
111110
},
112111
});

packages/SwingSet/src/devices/mailbox.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666

6767
import Nat from '@agoric/nat';
6868

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

132134
function populateFromData(data) {
133-
if (state.size) {
134-
throw new Error(`cannot populateFromData: outbox is not empty`);
135-
}
135+
assert(!state.size, X`cannot populateFromData: outbox is not empty`);
136136
for (const peer of Object.getOwnPropertyNames(data)) {
137137
const inout = getOrCreatePeer(peer);
138-
const d = data[peer];
138+
const dp = data[peer];
139139
importMailbox(
140140
{
141-
ack: d.inboundAck,
142-
outbox: d.outbox,
141+
ack: dp.inboundAck,
142+
outbox: dp.outbox,
143143
},
144144
inout,
145145
);
@@ -185,7 +185,7 @@ export function buildMailbox(state) {
185185
try {
186186
return Boolean(inboundCallback(peer, messages, ack));
187187
} catch (e) {
188-
throw new Error(`error in inboundCallback: ${e}`);
188+
assert.fail(X`error in inboundCallback: ${e}`);
189189
}
190190
}
191191

packages/SwingSet/src/devices/plugin-src.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
const { details: X } = assert;
6+
57
export function buildRootDeviceNode(tools) {
68
const { SO, getDeviceState, setDeviceState, endowments } = tools;
79
const restart = getDeviceState();
@@ -95,9 +97,7 @@ export function buildRootDeviceNode(tools) {
9597
function send(index, obj) {
9698
const mod = connectedMods[index];
9799
// console.info('send', index, obj, mod);
98-
if (!mod) {
99-
throw TypeError(`No module associated with ${index}`);
100-
}
100+
assert(mod, X`No module associated with ${index}`, TypeError);
101101
let sender = senders[index];
102102
if (!sender) {
103103
// Lazily create a fresh sender.
@@ -124,9 +124,7 @@ export function buildRootDeviceNode(tools) {
124124
connect,
125125
send,
126126
registerReceiver(receiver) {
127-
if (registeredReceiver) {
128-
throw Error(`registered receiver already set`);
129-
}
127+
assert(!registeredReceiver, X`registered receiver already set`);
130128
registeredReceiver = receiver;
131129
saveState();
132130
},

packages/SwingSet/src/devices/timer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Nat from '@agoric/nat';
22

3+
const { details: X } = assert;
4+
35
/**
46
* Endowments for a Timer device that can be made available to SwingSet vats.
57
*
@@ -23,7 +25,7 @@ export function buildTimer() {
2325
try {
2426
return Boolean(devicePollFunction(Nat(time)));
2527
} catch (e) {
26-
throw new Error(`error in devicePollFunction: ${e}`);
28+
assert.fail(X`error in devicePollFunction: ${e}`);
2729
}
2830
}
2931

packages/SwingSet/src/hostStorage.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { initSwingStore } from '@agoric/swing-store-simple';
22

3+
const { details: X } = assert;
4+
35
/*
46
The "Storage API" is a set of functions { has, getKeys, get, set, delete } that
57
work on string keys and accept string values. A lot of kernel-side code
@@ -97,21 +99,15 @@ export function buildHostDBInMemory(storage) {
9799
// after earlier changes have actually been applied, potentially leaving
98100
// the store in an indeterminate state. Problem? I suspect so...
99101
for (const c of changes) {
100-
if (`${c.op}` !== c.op) {
101-
throw new Error(`non-string c.op ${c.op}`);
102-
}
103-
if (`${c.key}` !== c.key) {
104-
throw new Error(`non-string c.key ${c.key}`);
105-
}
102+
assert(`${c.op}` === c.op, X`non-string c.op ${c.op}`);
103+
assert(`${c.key}` === c.key, X`non-string c.key ${c.key}`);
106104
if (c.op === 'set') {
107-
if (`${c.value}` !== c.value) {
108-
throw new Error(`non-string c.value ${c.value}`);
109-
}
105+
assert(`${c.value}` === c.value, X`non-string c.value ${c.value}`);
110106
storage.set(c.key, c.value);
111107
} else if (c.op === 'delete') {
112108
storage.delete(c.key);
113109
} else {
114-
throw new Error(`unknown c.op ${c.op}`);
110+
assert.fail(X`unknown c.op ${c.op}`);
115111
}
116112
}
117113
}

0 commit comments

Comments
 (0)