Skip to content

Commit ca0b874

Browse files
authored
refactor: use Fail template tag in ERTP and zoe (#6709)
* refactor: use Fail template tag in ERTP and zoe * refactor: repair damage
1 parent e74278c commit ca0b874

37 files changed

+164
-254
lines changed

Diff for: packages/ERTP/src/amountMath.js

+21-30
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { setMathHelpers } from './mathHelpers/setMathHelpers.js';
66
import { copySetMathHelpers } from './mathHelpers/copySetMathHelpers.js';
77
import { copyBagMathHelpers } from './mathHelpers/copyBagMathHelpers.js';
88

9-
const { details: X, quote: q } = assert;
9+
const { quote: q, Fail } = assert;
1010

1111
/**
1212
* Constants for the kinds of assets we support.
@@ -27,9 +27,7 @@ const assetKindNames = harden(Object.values(AssetKind).sort());
2727
*/
2828
const assertAssetKind = allegedAK => {
2929
assetKindNames.includes(allegedAK) ||
30-
assert.fail(
31-
X`The assetKind ${allegedAK} must be one of ${q(assetKindNames)}`,
32-
);
30+
Fail`The assetKind ${allegedAK} must be one of ${q(assetKindNames)}`;
3331
};
3432
harden(assertAssetKind);
3533

@@ -101,14 +99,14 @@ const assertValueGetAssetKind = value => {
10199
// @ts-expect-error cast
102100
return 'copyBag';
103101
}
104-
assert.fail(
105-
// TODO This isn't quite the right error message, in case valuePassStyle
106-
// is 'tagged'. We would need to distinguish what kind of tagged
107-
// object it is.
108-
// Also, this kind of manual listing is a maintenance hazard we
109-
// (TODO) will encounter when we extend the math helpers further.
110-
X`value ${value} must be a bigint, copySet, copyBag, or an array, not ${passStyle}`,
111-
);
102+
// TODO This isn't quite the right error message, in case valuePassStyle
103+
// is 'tagged'. We would need to distinguish what kind of tagged
104+
// object it is.
105+
// Also, this kind of manual listing is a maintenance hazard we
106+
// (TODO) will encounter when we extend the math helpers further.
107+
throw Fail`value ${value} must be a bigint, copySet, copyBag, or an array, not ${q(
108+
passStyle,
109+
)}`;
112110
};
113111

114112
/**
@@ -129,11 +127,10 @@ export const assertValueGetHelpers = value =>
129127
const optionalBrandCheck = (allegedBrand, brand) => {
130128
if (brand !== undefined) {
131129
assertRemotable(brand, 'brand');
132-
assert.equal(
133-
allegedBrand,
134-
brand,
135-
X`amount's brand ${allegedBrand} did not match expected brand ${brand}`,
136-
);
130+
allegedBrand === brand ||
131+
Fail`amount's brand ${q(allegedBrand)} did not match expected brand ${q(
132+
brand,
133+
)}`;
137134
}
138135
};
139136

@@ -153,18 +150,14 @@ const checkLRAndGetHelpers = (leftAmount, rightAmount, brand = undefined) => {
153150
assertRemotable(rightBrand, 'rightBrand');
154151
optionalBrandCheck(leftBrand, brand);
155152
optionalBrandCheck(rightBrand, brand);
156-
assert.equal(
157-
leftBrand,
158-
rightBrand,
159-
X`Brands in left ${leftBrand} and right ${rightBrand} should match but do not`,
160-
);
153+
leftBrand === rightBrand ||
154+
Fail`Brands in left ${q(leftBrand)} and right ${q(
155+
rightBrand,
156+
)} should match but do not`;
161157
const leftHelpers = assertValueGetHelpers(leftValue);
162158
const rightHelpers = assertValueGetHelpers(rightValue);
163-
assert.equal(
164-
leftHelpers,
165-
rightHelpers,
166-
X`The left ${leftAmount} and right amount ${rightAmount} had different assetKinds`,
167-
);
159+
leftHelpers === rightHelpers ||
160+
Fail`The left ${leftAmount} and right amount ${rightAmount} had different assetKinds`;
168161
return leftHelpers;
169162
};
170163

@@ -219,9 +212,7 @@ const AmountMath = {
219212
assertRecord(allegedAmount, 'amount');
220213
const { brand: allegedBrand, value: allegedValue } = allegedAmount;
221214
brand === allegedBrand ||
222-
assert.fail(
223-
X`The brand in the allegedAmount ${allegedAmount} in 'coerce' didn't match the specified brand ${brand}.`,
224-
);
215+
Fail`The brand in the allegedAmount ${allegedAmount} in 'coerce' didn't match the specified brand ${brand}.`;
225216
// Will throw on inappropriate value
226217
return AmountMath.make(brand, allegedValue);
227218
},

Diff for: packages/ERTP/src/displayInfo.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, details as X } from '@agoric/assert';
1+
import { Fail } from '@agoric/assert';
22
import { fit } from '@agoric/store';
33

44
import { DisplayInfoShape } from './typeGuards.js';
@@ -13,17 +13,13 @@ export const coerceDisplayInfo = (allegedDisplayInfo, assetKind) => {
1313

1414
if (allegedDisplayInfo.assetKind !== undefined) {
1515
allegedDisplayInfo.assetKind === assetKind ||
16-
assert.fail(
17-
X`displayInfo.assetKind was present (${allegedDisplayInfo.assetKind}) and did not match the assetKind argument (${assetKind})`,
18-
);
16+
Fail`displayInfo.assetKind was present (${allegedDisplayInfo.assetKind}) and did not match the assetKind argument (${assetKind})`;
1917
}
2018
const displayInfo = harden({ ...allegedDisplayInfo, assetKind });
2119

2220
if (displayInfo.decimalPlaces !== undefined) {
2321
Number.isSafeInteger(displayInfo.decimalPlaces) ||
24-
assert.fail(
25-
X`decimalPlaces ${displayInfo.decimalPlaces} is not a safe integer`,
26-
);
22+
Fail`decimalPlaces ${displayInfo.decimalPlaces} is not a safe integer`;
2723
}
2824

2925
return displayInfo;

Diff for: packages/ERTP/src/mathHelpers/natMathHelpers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Nat, isNat } from '@agoric/nat';
22

33
import '../types-ambient.js';
44

5-
const { details: X } = assert;
5+
const { Fail } = assert;
66
const empty = 0n;
77

88
/**
@@ -21,7 +21,7 @@ export const natMathHelpers = harden({
2121
doCoerce: nat => {
2222
// TODO: tighten the definition of Nat in @agoric/nat to throw on `number`
2323
assert.typeof(nat, 'bigint');
24-
assert(isNat(nat), X`value ${nat} must be a natural number`);
24+
isNat(nat) || Fail`value ${nat} must be a natural number`;
2525
return Nat(nat);
2626
},
2727
doMakeEmpty: () => empty,

Diff for: packages/ERTP/src/paymentLedger.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ import { BrandI, makeIssuerInterfaces } from './typeGuards.js';
1515

1616
/** @typedef {import('@agoric/vat-data').Baggage} Baggage */
1717

18-
const { details: X, quote: q } = assert;
18+
const { details: X, quote: q, Fail } = assert;
1919

2020
const amountShapeFromElementShape = (brand, assetKind, elementShape) => {
2121
let valueShape;
2222
switch (assetKind) {
2323
case 'nat': {
2424
valueShape = M.nat();
2525
elementShape === undefined ||
26-
assert.fail(
27-
X`Fungible assets cannot have an elementShape: ${q(elementShape)}`,
28-
);
26+
Fail`Fungible assets cannot have an elementShape: ${q(elementShape)}`;
2927
break;
3028
}
3129
case 'set': {
@@ -53,7 +51,7 @@ const amountShapeFromElementShape = (brand, assetKind, elementShape) => {
5351
break;
5452
}
5553
default: {
56-
assert.fail(X`unexpected asset kind ${q(assetKind)}`);
54+
Fail`unexpected asset kind ${q(assetKind)}`;
5755
}
5856
}
5957

@@ -230,11 +228,9 @@ export const vivifyPaymentLedger = (
230228
*/
231229
const assertLivePayment = payment => {
232230
paymentLedger.has(payment) ||
233-
assert.fail(
234-
X`${payment} was not a live payment for brand ${q(
235-
brand,
236-
)}. It could be a used-up payment, a payment for another brand, or it might not be a payment at all.`,
237-
);
231+
Fail`${payment} was not a live payment for brand ${q(
232+
brand,
233+
)}. It could be a used-up payment, a payment for another brand, or it might not be a payment at all.`;
238234
};
239235

240236
/**
@@ -266,7 +262,7 @@ export const vivifyPaymentLedger = (
266262
const antiAliasingStore = new Set();
267263
payments.forEach(payment => {
268264
!antiAliasingStore.has(payment) ||
269-
assert.fail(X`same payment ${payment} seen twice`);
265+
Fail`same payment ${payment} seen twice`;
270266
antiAliasingStore.add(payment);
271267
});
272268
}
@@ -277,7 +273,7 @@ export const vivifyPaymentLedger = (
277273

278274
// Invariant check
279275
isEqual(total, newTotal) ||
280-
assert.fail(X`rights were not conserved: ${total} vs ${newTotal}`);
276+
Fail`rights were not conserved: ${total} vs ${newTotal}`;
281277

282278
let newPayments;
283279
try {
@@ -354,9 +350,7 @@ export const vivifyPaymentLedger = (
354350
) => {
355351
amount = coerce(amount);
356352
AmountMath.isGTE(currentBalance, amount) ||
357-
assert.fail(
358-
X`Withdrawal of ${amount} failed because the purse only contained ${currentBalance}`,
359-
);
353+
Fail`Withdrawal of ${amount} failed because the purse only contained ${currentBalance}`;
360354
const newPurseBalance = subtract(currentBalance, amount);
361355

362356
const payment = makePayment();

Diff for: packages/ERTP/src/purse.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { vivifyFarClassKit, makeScalarBigSetStore } from '@agoric/vat-data';
22
import { AmountMath } from './amountMath.js';
33
import { makeTransientNotifierKit } from './transientNotifier.js';
44

5-
const { details: X } = assert;
5+
const { Fail } = assert;
66

77
export const vivifyPurseKind = (
88
issuerBaggage,
@@ -99,9 +99,7 @@ export const vivifyPurseKind = (
9999
amount = AmountMath.add(amount, delta, brand);
100100
}
101101
state.recoverySet.getSize() === 0 ||
102-
assert.fail(
103-
X`internal: Remaining unrecovered payments: ${facets.purse.getRecoverySet()}`,
104-
);
102+
Fail`internal: Remaining unrecovered payments: ${facets.purse.getRecoverySet()}`;
105103
return amount;
106104
},
107105
},

Diff for: packages/inter-protocol/test/amm/constantProduct/test-calcDeltaX.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const doTest = (t, x, y, deltaY, expectedDeltaX) => {
1616

1717
test('0, 0, 0, 0', t => {
1818
t.throws(() => doTest(t, 0n, 0n, 0n, 0n), {
19-
message: 'No infinite ratios! Denominator was 0/"[Alleged: BLD brand]"',
19+
message: 'No infinite ratios! Denominator was 0 "[Alleged: BLD brand]"',
2020
});
2121
});
2222

@@ -28,7 +28,7 @@ test('0, 0, 1, 0', t => {
2828

2929
test('1, 0, 0, 0', t => {
3030
t.throws(() => doTest(t, 1n, 0n, 0n, 0n), {
31-
message: 'No infinite ratios! Denominator was 0/"[Alleged: BLD brand]"',
31+
message: 'No infinite ratios! Denominator was 0 "[Alleged: BLD brand]"',
3232
});
3333
});
3434

@@ -42,7 +42,7 @@ test('1, 1, 0, 0', t => {
4242

4343
test('1, 1, 1, 0', t => {
4444
t.throws(() => doTest(t, 1n, 1n, 1n, 0n), {
45-
message: 'No infinite ratios! Denominator was 0/"[Alleged: BLD brand]"',
45+
message: 'No infinite ratios! Denominator was 0 "[Alleged: BLD brand]"',
4646
});
4747
});
4848

Diff for: packages/inter-protocol/test/amm/constantProduct/test-calcDeltaY.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const doTest = (t, x, y, deltaX, expectedDeltaY) => {
1717
// deltaXPlusX is 0
1818
test('0, 0, 0, 0', t => {
1919
t.throws(() => doTest(t, 0n, 0n, 0n, 0n), {
20-
message: 'No infinite ratios! Denominator was 0/"[Alleged: RUN brand]"',
20+
message: 'No infinite ratios! Denominator was 0 "[Alleged: RUN brand]"',
2121
});
2222
});
2323

@@ -32,7 +32,7 @@ test('1, 0, 0, 0', t => {
3232
// deltaXPlusX is 0
3333
test('0, 1, 0, 0', t => {
3434
t.throws(() => doTest(t, 0n, 1n, 0n, 0n), {
35-
message: 'No infinite ratios! Denominator was 0/"[Alleged: RUN brand]"',
35+
message: 'No infinite ratios! Denominator was 0 "[Alleged: RUN brand]"',
3636
});
3737
});
3838

Diff for: packages/ui-components/src/ratio.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const makeRatio = (
2525
denominatorBrand = numeratorBrand,
2626
) => {
2727
denominator > 0n ||
28-
Fail`No infinite ratios! Denominator was 0/${q(denominatorBrand)}`;
28+
Fail`No infinite ratios! Denominator was 0 ${q(denominatorBrand)}`;
2929

3030
// @ts-expect-error cast to return type because make() ensures
3131
return harden({

Diff for: packages/zoe/src/cleanProposal.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, details as X, q } from '@agoric/assert';
1+
import { assert, q, Fail } from '@agoric/assert';
22
import { AmountMath, getAssetKind } from '@agoric/ertp';
33
import { assertRecord } from '@endo/marshal';
44
import { assertKey, assertPattern, fit, isKey } from '@agoric/store';
@@ -25,19 +25,15 @@ const firstCapASCII = /^[A-Z][a-zA-Z0-9_$]*$/;
2525
export const assertKeywordName = keyword => {
2626
assert.typeof(keyword, 'string');
2727
keyword.length <= MAX_KEYWORD_LENGTH ||
28-
assert.fail(
29-
X`keyword ${q(keyword)} exceeded maximum length ${q(
30-
MAX_KEYWORD_LENGTH,
31-
)} characters; got ${keyword.length}`,
32-
);
33-
assert(
34-
firstCapASCII.test(keyword),
35-
X`keyword ${q(
28+
Fail`keyword ${q(keyword)} exceeded maximum length ${q(
29+
MAX_KEYWORD_LENGTH,
30+
)} characters; got ${keyword.length}`;
31+
firstCapASCII.test(keyword) ||
32+
Fail`keyword ${q(
3633
keyword,
37-
)} must be an ascii identifier starting with upper case.`,
38-
);
34+
)} must be an ascii identifier starting with upper case.`;
3935
(keyword !== 'NaN' && keyword !== 'Infinity') ||
40-
assert.fail(X`keyword ${q(keyword)} must not be a number's name`);
36+
Fail`keyword ${q(keyword)} must not be a number's name`;
4137
};
4238

4339
/**
@@ -72,9 +68,7 @@ export const coerceAmountPatternKeywordRecord = (
7268
// TODO: replace this assertion with a check of the assetKind
7369
// property on the brand, when that exists.
7470
assetKind === brandAssetKind ||
75-
assert.fail(
76-
X`The amount ${amount} did not have the assetKind of the brand ${brandAssetKind}`,
77-
);
71+
Fail`The amount ${amount} did not have the assetKind of the brand ${brandAssetKind}`;
7872
return AmountMath.coerce(amount.brand, amount);
7973
} else {
8074
assertPattern(amount);
@@ -106,7 +100,7 @@ export const coerceAmountKeywordRecord = (
106100
* @param {ExitRule} exit
107101
*/
108102
const assertExit = exit =>
109-
assert(ownKeys(exit).length === 1, X`exit ${exit} should only have one key`);
103+
ownKeys(exit).length === 1 || Fail`exit ${exit} should only have one key`;
110104

111105
/**
112106
* check that keyword is not in both 'want' and 'give'.
@@ -120,7 +114,7 @@ const assertKeywordNotInBoth = (want, give) => {
120114

121115
giveKeywords.forEach(keyword => {
122116
!wantKeywordSet.has(keyword) ||
123-
assert.fail(X`a keyword cannot be in both 'want' and 'give'`);
117+
Fail`a keyword cannot be in both 'want' and 'give'`;
124118
});
125119
};
126120

@@ -150,9 +144,7 @@ export const cleanProposal = (proposal, getAssetKindByBrand) => {
150144
...rest
151145
} = proposal;
152146
ownKeys(rest).length === 0 ||
153-
assert.fail(
154-
X`${proposal} - Must only have want:, give:, exit: properties: ${rest}`,
155-
);
147+
Fail`${proposal} - Must only have want:, give:, exit: properties: ${rest}`;
156148

157149
const cleanedWant = coerceAmountPatternKeywordRecord(
158150
want,

Diff for: packages/zoe/src/contractFacet/allocationMath.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AmountMath } from '@agoric/ertp';
22

3-
const { details: X, quote: q } = assert;
3+
const { Fail, quote: q } = assert;
44

55
/**
66
* @callback Operation
@@ -63,18 +63,14 @@ const subtract = (amount, amountToSubtract, keyword) => {
6363
if (amount === undefined) {
6464
// TypeScript confused about `||` control flow so use `if` instead
6565
// https://github.com/microsoft/TypeScript/issues/50739
66-
assert.fail(
67-
X`The amount could not be subtracted from the allocation because the allocation did not have an amount under the keyword ${q(
68-
keyword,
69-
)}.`,
70-
);
66+
throw Fail`The amount could not be subtracted from the allocation because the allocation did not have an amount under the keyword ${q(
67+
keyword,
68+
)}.`;
7169
}
7270
AmountMath.isGTE(amount, amountToSubtract) ||
73-
assert.fail(
74-
X`The amount to be subtracted ${amountToSubtract} was greater than the allocation's amount ${amount} for the keyword ${q(
75-
keyword,
76-
)}`,
77-
);
71+
Fail`The amount to be subtracted ${amountToSubtract} was greater than the allocation's amount ${amount} for the keyword ${q(
72+
keyword,
73+
)}`;
7874
return AmountMath.subtract(amount, amountToSubtract);
7975
}
8076
};

0 commit comments

Comments
 (0)