Skip to content

Commit 07d5638

Browse files
authored
refactor: shorten used failure idiom (#2852)
Closes: #XXXX Refs: Agoric/agoric-sdk#11458 ## Description Whenever we say, for example, ```js assert.fail(X`...`) ``` we should instead say ```js Fail`...` ``` This PR is a refactor doing exactly this. ### Security Considerations none ### Scaling Considerations none ### Documentation Considerations none ### Testing Considerations Most of the uses were in tests. ### Compatibility Considerations none ### Upgrade Considerations none
1 parent 378a5aa commit 07d5638

10 files changed

+53
-58
lines changed

packages/pass-style/src/byteArray.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { X } from '@endo/errors';
1+
import { X, Fail } from '@endo/errors';
22

33
/**
44
* @import {PassStyleHelper} from './internal-types.js';
@@ -39,7 +39,7 @@ export const ByteArrayHelper = harden({
3939
getPrototypeOf(candidate) === ImmutableArrayBufferPrototype ||
4040
assert.fail(X`Malformed ByteArray ${candidate}`, TypeError);
4141
apply(immutableGetter, candidate, []) ||
42-
assert.fail(X`Must be an immutable ArrayBuffer: ${candidate}`);
42+
Fail`Must be an immutable ArrayBuffer: ${candidate}`;
4343
ownKeys(candidate).length === 0 ||
4444
assert.fail(
4545
X`ByteArrays must not have own properties: ${candidate}`,

packages/ses/test/error/assert-log.test.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import test from 'ava';
22
import { assertLogs, throwsAndLogs } from './_throws-and-logs.js';
33
import { assert } from '../../src/error/assert.js';
44

5-
const { details: X, quote: q, bare: b, error: makeError } = assert;
5+
const { details: X, quote: q, bare: b, error: makeError, Fail } = assert;
66

77
// Self-test of the example from the throwsAndLogs comment.
88
test('throwsAndLogs with data', t => {
@@ -86,11 +86,11 @@ test('causal tree', t => {
8686
const fooErr = SyntaxError('foo');
8787
let err1;
8888
try {
89-
assert.fail(X`synful ${fooErr}`);
89+
Fail`synful ${fooErr}`;
9090
} catch (e1) {
9191
err1 = e1;
9292
}
93-
assert.fail(X`because ${err1}`);
93+
Fail`because ${err1}`;
9494
},
9595
/because/,
9696
[['log', 'Caught', Error]],
@@ -101,11 +101,11 @@ test('causal tree', t => {
101101
const fooErr = SyntaxError('foo');
102102
let err1;
103103
try {
104-
assert.fail(X`synful ${fooErr}`);
104+
Fail`synful ${fooErr}`;
105105
} catch (e1) {
106106
err1 = e1;
107107
}
108-
assert.fail(X`because ${err1}`);
108+
Fail`because ${err1}`;
109109
},
110110
/because/,
111111
[
@@ -149,12 +149,12 @@ test('a causal tree falls silently', t => {
149149
const fooErr = SyntaxError('foo');
150150
let err1;
151151
try {
152-
assert.fail(X`synful ${fooErr}`);
152+
Fail`synful ${fooErr}`;
153153
} catch (e1) {
154154
err1 = e1;
155155
}
156156
try {
157-
assert.fail(X`because ${err1}`);
157+
Fail`because ${err1}`;
158158
} catch (e2) {
159159
t.assert(e2 instanceof Error);
160160
}
@@ -165,12 +165,12 @@ test('a causal tree falls silently', t => {
165165
const fooErr = SyntaxError('foo');
166166
let err1;
167167
try {
168-
assert.fail(X`synful ${fooErr}`);
168+
Fail`synful ${fooErr}`;
169169
} catch (e1) {
170170
err1 = e1;
171171
}
172172
try {
173-
assert.fail(X`because ${err1}`);
173+
Fail`because ${err1}`;
174174
} catch (e2) {
175175
t.assert(e2 instanceof Error);
176176
}
@@ -336,15 +336,12 @@ test('makeError named', t => {
336336
});
337337

338338
test('assert.quote', t => {
339+
throwsAndLogs(t, () => Fail`<${'bar'},${q('baz')}>`, /<\(a string\),"baz">/, [
340+
['log', 'Caught', Error],
341+
]);
339342
throwsAndLogs(
340343
t,
341-
() => assert.fail(X`<${'bar'},${q('baz')}>`),
342-
/<\(a string\),"baz">/,
343-
[['log', 'Caught', Error]],
344-
);
345-
throwsAndLogs(
346-
t,
347-
() => assert.fail(X`<${'bar'},${q('baz')}>`),
344+
() => Fail`<${'bar'},${q('baz')}>`,
348345
/<\(a string\),"baz">/,
349346
[
350347
['log', 'Caught', '(Error#1)'],
@@ -355,47 +352,45 @@ test('assert.quote', t => {
355352
);
356353
/** @type {any[]} */
357354
const list = ['a', 'b', 'c'];
358-
throwsAndLogs(t, () => assert.fail(X`${q(list)}`), /\["a","b","c"\]/, [
355+
throwsAndLogs(t, () => Fail`${q(list)}`, /\["a","b","c"\]/, [
359356
['log', 'Caught', Error],
360357
]);
361358
const repeat = { x: list, y: list };
362359
throwsAndLogs(
363360
t,
364-
() => assert.fail(X`${q(repeat)}`),
361+
() => Fail`${q(repeat)}`,
365362
/{"x":\["a","b","c"\],"y":"\[Seen\]"}/,
366363
[['log', 'Caught', Error]],
367364
);
368365
// Make it into a cycle
369366
list[1] = list;
370-
throwsAndLogs(t, () => assert.fail(X`${q(list)}`), /\["a","\[Seen\]","c"\]/, [
367+
throwsAndLogs(t, () => Fail`${q(list)}`, /\["a","\[Seen\]","c"\]/, [
371368
['log', 'Caught', Error],
372369
]);
373370
throwsAndLogs(
374371
t,
375-
() => assert.fail(X`${q(repeat)}`),
372+
() => Fail`${q(repeat)}`,
376373
/{"x":\["a","\[Seen\]","c"\],"y":"\[Seen\]"}/,
377374
[['log', 'Caught', Error]],
378375
);
379376
});
380377

381378
test('assert.bare', t => {
382-
throwsAndLogs(t, () => assert.fail(X`${b('foo')}`), 'foo', [
383-
['log', 'Caught', Error],
384-
]);
379+
throwsAndLogs(t, () => Fail`${b('foo')}`, 'foo', [['log', 'Caught', Error]]);
385380
// Spaces are allowed in bare values.
386-
throwsAndLogs(t, () => assert.fail(X`${b('foo bar')}`), 'foo bar', [
381+
throwsAndLogs(t, () => Fail`${b('foo bar')}`, 'foo bar', [
387382
['log', 'Caught', Error],
388383
]);
389384
// Multiple consecutive spaces are disallowed and fall back to quote.
390-
throwsAndLogs(t, () => assert.fail(X`${b('foo bar')}`), '"foo bar"', [
385+
throwsAndLogs(t, () => Fail`${b('foo bar')}`, '"foo bar"', [
391386
['log', 'Caught', Error],
392387
]);
393388
// Strings with non-word punctuation also fall back.
394-
throwsAndLogs(t, () => assert.fail(X`${b('foo%bar')}`), '"foo%bar"', [
389+
throwsAndLogs(t, () => Fail`${b('foo%bar')}`, '"foo%bar"', [
395390
['log', 'Caught', Error],
396391
]);
397392
// Non-strings also fall back.
398-
throwsAndLogs(t, () => assert.fail(X`${b(undefined)}`), '"[undefined]"', [
393+
throwsAndLogs(t, () => Fail`${b(undefined)}`, '"[undefined]"', [
399394
['log', 'Caught', Error],
400395
]);
401396
});

packages/ses/test/error/tame-console-unfilteredError.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const originalConsole = console;
66

77
lockdown({ errorTaming: 'safe', stackFiltering: 'verbose' });
88

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

1111
test('ava message disclosure quiet', t => {
12-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
12+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1313
message: /a secret \(a number\) and a public 777/,
1414
});
1515
});
@@ -50,7 +50,7 @@ test('assert - safe', t => {
5050
try {
5151
const obj = {};
5252
const fooErr = SyntaxError('foo');
53-
assert.fail(X`caused by ${fooErr},${obj}`);
53+
Fail`caused by ${fooErr},${obj}`;
5454
} catch (barErr) {
5555
console.error('bar happens', barErr);
5656
}
@@ -61,7 +61,7 @@ test('assert - unlogged safe', t => {
6161
t.throws(() => {
6262
const obj = {};
6363
const fooErr = SyntaxError('foo');
64-
assert.fail(X`caused by ${fooErr},${obj}`);
64+
Fail`caused by ${fooErr},${obj}`;
6565
});
6666
});
6767

packages/ses/test/error/tame-console-unsafe-unfilteredError.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ lockdown({
1010
stackFiltering: 'verbose',
1111
});
1212

13-
const { details: X, quote: q, note: annotateError } = assert;
13+
const { details: X, quote: q, note: annotateError, Fail } = assert;
1414

1515
test('ava message disclosure quiet', t => {
16-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
16+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1717
message: /a secret \(a number\) and a public 777/,
1818
});
1919
});
@@ -57,7 +57,7 @@ test('assert - unsafe', t => {
5757
try {
5858
const obj = {};
5959
const fooErr = SyntaxError('foo');
60-
assert.fail(X`caused by ${fooErr},${obj}`);
60+
Fail`caused by ${fooErr},${obj}`;
6161
} catch (barErr) {
6262
console.error('bar happens', barErr);
6363
}
@@ -68,7 +68,7 @@ test('assert - unlogged unsafe', t => {
6868
t.throws(() => {
6969
const obj = {};
7070
const fooErr = SyntaxError('foo');
71-
assert.fail(X`caused by ${fooErr},${obj}`);
71+
Fail`caused by ${fooErr},${obj}`;
7272
});
7373
});
7474

packages/ses/test/error/tame-console-unsafe-unsafeError-unfilteredError.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ lockdown({
1212
});
1313

1414
// Grab `details` only after lockdown
15-
const { details: X, quote: q, note: annotateError } = assert;
15+
const { details: X, quote: q, note: annotateError, Fail } = assert;
1616

1717
test('ava message disclosure blabs', t => {
18-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
18+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1919
message: /a secret 666 and a public 777/,
2020
});
2121
});
@@ -59,7 +59,7 @@ test('assert - unsafe', t => {
5959
try {
6060
const obj = {};
6161
const fooErr = SyntaxError('foo');
62-
assert.fail(X`caused by ${fooErr},${obj}`);
62+
Fail`caused by ${fooErr},${obj}`;
6363
} catch (barErr) {
6464
console.error('bar happens', barErr);
6565
}
@@ -70,7 +70,7 @@ test('assert - unlogged unsafe', t => {
7070
t.throws(() => {
7171
const obj = {};
7272
const fooErr = SyntaxError('foo');
73-
assert.fail(X`caused by ${fooErr},${obj}`);
73+
Fail`caused by ${fooErr},${obj}`;
7474
});
7575
});
7676

packages/ses/test/error/tame-console-unsafe-unsafeError.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ lockdown({
1111
});
1212

1313
// Grab `details` only after lockdown
14-
const { details: X, quote: q, note: annotateError } = assert;
14+
const { details: X, quote: q, note: annotateError, Fail } = assert;
1515

1616
test('ava message disclosure blabs', t => {
17-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
17+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1818
message: /a secret 666 and a public 777/,
1919
});
2020
});
@@ -58,7 +58,7 @@ test('assert - unsafe', t => {
5858
try {
5959
const obj = {};
6060
const fooErr = SyntaxError('foo');
61-
assert.fail(X`caused by ${fooErr},${obj}`);
61+
Fail`caused by ${fooErr},${obj}`;
6262
} catch (barErr) {
6363
console.error('bar happens', barErr);
6464
}
@@ -69,7 +69,7 @@ test('assert - unlogged unsafe', t => {
6969
t.throws(() => {
7070
const obj = {};
7171
const fooErr = SyntaxError('foo');
72-
assert.fail(X`caused by ${fooErr},${obj}`);
72+
Fail`caused by ${fooErr},${obj}`;
7373
});
7474
});
7575

packages/ses/test/error/tame-console-unsafe.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const originalConsole = console;
66

77
lockdown({ errorTaming: 'safe', consoleTaming: 'unsafe' });
88

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

1111
test('ava message disclosure quiet', t => {
12-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
12+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1313
message: /a secret \(a number\) and a public 777/,
1414
});
1515
});
@@ -54,7 +54,7 @@ test('assert - unsafe', t => {
5454
try {
5555
const obj = {};
5656
const fooErr = SyntaxError('foo');
57-
assert.fail(X`caused by ${fooErr},${obj}`);
57+
Fail`caused by ${fooErr},${obj}`;
5858
} catch (barErr) {
5959
console.error('bar happens', barErr);
6060
}
@@ -65,7 +65,7 @@ test('assert - unlogged unsafe', t => {
6565
t.throws(() => {
6666
const obj = {};
6767
const fooErr = SyntaxError('foo');
68-
assert.fail(X`caused by ${fooErr},${obj}`);
68+
Fail`caused by ${fooErr},${obj}`;
6969
});
7070
});
7171

packages/ses/test/error/tame-console-unsafeError.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const originalConsole = console;
77
lockdown({ errorTaming: 'unsafe' });
88

99
// Grab `details` only after lockdown
10-
const { details: X, quote: q, note: annotateError } = assert;
10+
const { details: X, quote: q, note: annotateError, Fail } = assert;
1111

1212
test('ava message disclosure blabs', t => {
13-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
13+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1414
message: /a secret 666 and a public 777/,
1515
});
1616
});
@@ -51,7 +51,7 @@ test('assert - safe', t => {
5151
try {
5252
const obj = {};
5353
const fooErr = SyntaxError('foo');
54-
assert.fail(X`caused by ${fooErr},${obj}`);
54+
Fail`caused by ${fooErr},${obj}`;
5555
} catch (barErr) {
5656
console.error('bar happens', barErr);
5757
}
@@ -62,7 +62,7 @@ test('assert - unlogged safe', t => {
6262
t.throws(() => {
6363
const obj = {};
6464
const fooErr = SyntaxError('foo');
65-
assert.fail(X`caused by ${fooErr},${obj}`);
65+
Fail`caused by ${fooErr},${obj}`;
6666
});
6767
});
6868

packages/ses/test/error/tame-console.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const originalConsole = console;
66

77
lockdown({ errorTaming: 'safe' });
88

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

1111
test('ava message disclosure default', t => {
12-
t.throws(() => assert.fail(X`a secret ${666} and a public ${q(777)}`), {
12+
t.throws(() => Fail`a secret ${666} and a public ${q(777)}`, {
1313
message: /a secret \(a number\) and a public 777/,
1414
});
1515
});
@@ -58,7 +58,7 @@ test('assert - safe', t => {
5858
try {
5959
const obj = {};
6060
const fooErr = SyntaxError('foo');
61-
assert.fail(X`caused by ${fooErr},${obj}`);
61+
Fail`caused by ${fooErr},${obj}`;
6262
} catch (barErr) {
6363
console.error('bar happens', barErr);
6464
}
@@ -73,7 +73,7 @@ test('assert - unlogged safe', t => {
7373
t.throws(() => {
7474
const obj = {};
7575
const fooErr = SyntaxError('foo');
76-
assert.fail(X`caused by ${fooErr},${obj}`);
76+
Fail`caused by ${fooErr},${obj}`;
7777
});
7878
});
7979

packages/ses/types.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ expectType<Error>(
224224
}),
225225
);
226226

227-
expectType<never>(assert.fail(X`details are ${stringable}`));
227+
expectType<never>(Fail`details are ${stringable}`);
228228

229229
// eslint-disable-next-line no-unreachable
230230
expectType<never>(Fail`details are ${stringable}`);

0 commit comments

Comments
 (0)