Skip to content

Commit

Permalink
fix: better mismatch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Aug 16, 2022
1 parent b85d851 commit 1014ae5
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 167 deletions.
24 changes: 16 additions & 8 deletions packages/ERTP/test/unitTests/test-inputValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('makeIssuerKit bad assetKind', async t => {
// @ts-expect-error Intentional wrong type for testing
t.throws(() => makeIssuerKit('myTokens', 'somethingWrong'), {
message:
/The assetKind "somethingWrong" must be one of \["copyBag","copySet","nat","set"\]/,
'The assetKind "somethingWrong" must be one of ["copyBag","copySet","nat","set"]',
});
});

Expand All @@ -32,7 +32,10 @@ test('makeIssuerKit bad displayInfo.decimalPlaces', async t => {
// @ts-expect-error Intentional wrong type for testing
harden({ decimalPlaces: 'hello' }),
),
{ message: /^displayInfo: "hello" - Must be >= -100$/ },
{
message:
'displayInfo: optional-parts: decimalPlaces: "hello" - Must be >= -100',
},
);

t.throws(
Expand All @@ -58,13 +61,19 @@ test('makeIssuerKit bad displayInfo.decimalPlaces', async t => {
t.throws(
() =>
makeIssuerKit('myTokens', AssetKind.NAT, harden({ decimalPlaces: 101 })),
{ message: /^displayInfo: 101 - Must be <= 100$/ },
{
message:
'displayInfo: optional-parts: decimalPlaces: 101 - Must be <= 100',
},
);

t.throws(
() =>
makeIssuerKit('myTokens', AssetKind.NAT, harden({ decimalPlaces: -101 })),
{ message: /^displayInfo: -101 - Must be >= -100$/ },
{
message:
'displayInfo: optional-parts: decimalPlaces: -101 - Must be >= -100',
},
);
});

Expand All @@ -81,7 +90,7 @@ test('makeIssuerKit bad displayInfo.assetKind', async t => {
),
{
message:
/^displayInfo: "something" - Must match one of \["nat","set","copySet","copyBag"\]$/,
'displayInfo: optional-parts: assetKind: "something" - Must match one of ["nat","set","copySet","copyBag"]',
},
);
});
Expand All @@ -99,7 +108,7 @@ test('makeIssuerKit bad displayInfo.whatever', async t => {
),
{
message:
/^displayInfo: Remainder \{"whatever":"something"\} - Must match \{\}$/,
'displayInfo: rest-parts: {"whatever":"something"} - Must be: {}',
},
);
});
Expand All @@ -114,8 +123,7 @@ test('makeIssuerKit malicious displayInfo', async t => {
'badness',
),
{
message:
/^displayInfo: "badness" - Must have shape of base: "copyRecord"$/,
message: 'displayInfo: string "badness" - Must be a copyRecord',
},
);
});
Expand Down
5 changes: 2 additions & 3 deletions packages/ERTP/test/unitTests/test-issuerObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ test('bad display info', t => {
const displayInfo = harden({ somethingUnexpected: 3 });
// @ts-expect-error deliberate invalid arguments for testing
t.throws(() => makeIssuerKit('fungible', AssetKind.NAT, displayInfo), {
message:
/^displayInfo: Remainder \{"somethingUnexpected":3\} - Must match \{\}$/,
message: 'displayInfo: rest-parts: {"somethingUnexpected":3} - Must be: {}',
});
});

Expand Down Expand Up @@ -434,7 +433,7 @@ test('issuer.combine bad payments', async t => {
await t.throwsAsync(
() => E(issuer).combine(payments),
{
message: /"\[Alleged: other fungible payment\]"/,
message: /.* "\[Alleged: other fungible payment\]"/,
},
'payment from other mint is not found',
);
Expand Down
3 changes: 2 additions & 1 deletion packages/ERTP/test/unitTests/test-mintObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ test('mint.mintPayment set w strings AssetKind', async t => {

const badAmount = AmountMath.make(brand, harden([['badElement']]));
t.throws(() => mint.mintPayment(badAmount), {
message: / - Must have passStyle or tag "string"/,
message:
'minted amount: value: [0]: copyArray ["badElement"] - Must be a string',
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ test('add wrong liquidity', async t => {
);

await t.throwsAsync(() => E(addLiquiditySeatBreaking).getOfferResult(), {
message: /liquidity brand must be "\[Alleged: MoolaLiquidity brand\]"/,
message: 'liquidity brand must be "[Alleged: MoolaLiquidity brand]"',
});

await E(addLiquiditySeatBreaking).getPayouts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,10 @@ test('addVaultType: extra, unexpected params', async t => {
await t.throwsAsync(
// @ts-expect-error bad args
E(vaultFactory).addVaultType(chit.issuer, 'Chit', missingParams),
{ message: /Must have same property names/ },
{
message:
/initialParamValues: required-parts: .* - Must have missing properties \["interestRate"\]/,
},
);

const actual = await E(vaultFactory).addVaultType(
Expand Down
8 changes: 5 additions & 3 deletions packages/store/src/patterns/match-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ const { details: X } = assert;

/**
* @param {Error} innerErr
* @param {string} label
* @param {string|number} label
* @param {ErrorConstructor=} ErrorConstructor
* @returns {never}
*/
export const throwLabeled = (innerErr, label, ErrorConstructor = undefined) => {
if (typeof label === 'number') {
label = `[${label}]`;
}
const outerErr = assert.error(
`${label}: ${innerErr.message}`,
ErrorConstructor,
Expand All @@ -23,14 +26,13 @@ harden(throwLabeled);
* @template A,R
* @param {(...args: A) => R} func
* @param {A} args
* @param {string} [label]
* @param {string|number} [label]
* @returns {R}
*/
export const applyLabelingError = (func, args, label = undefined) => {
if (label === undefined) {
return func(...args);
}
assert.typeof(label, 'string');
let result;
try {
result = func(...args);
Expand Down
Loading

0 comments on commit 1014ae5

Please sign in to comment.