Skip to content

Commit

Permalink
fix: improved error message when eventual send target is undefined (#…
Browse files Browse the repository at this point in the history
…1847)

* fix: improved error message when eventual send target is undefined

* fix: fix hack to recognize new error message

* fix: improve error message for invoking object as function; add test
  • Loading branch information
Chris-Hibbert authored Oct 8, 2020
1 parent 01afe2d commit f33d30e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/src/vats/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const rethrowUnlessMissing = err => {
// exists.
if (
!(err instanceof TypeError) ||
!err.message.match(/target\[.*\] does not exist|is not a function$/)
!err.message.match(/target has no method|is not a function$/)
) {
throw err;
}
Expand Down
13 changes: 6 additions & 7 deletions packages/eventual-send/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,27 +361,26 @@ export function makeHandledPromise() {
if (method === undefined || method === null) {
if (!(t instanceof Function)) {
const ftype = typeof t;
throw TypeError(`target is not a function, typeof is ${ftype}`);
throw TypeError(
`Cannot invoke target as a function, the type is ${ftype}`,
);
}
return t(...args);
}
if (!t) {
const ftype = typeof t;
throw TypeError(
`target cannot contain [${q(method)}], typeof is ${ftype}`,
`Cannot deliver ${q(method)} to target; typeof target is "${ftype}"`,
);
}
if (!(method in t)) {
const names = Object.getOwnPropertyNames(t).sort();
throw TypeError(`target[${q(method)}] does not exist, has ${names}`);
throw TypeError(`target has no method ${q(method)}, has [${names}]`);
}
if (!(t[method] instanceof Function)) {
const ftype = typeof t[method];
const names = Object.getOwnPropertyNames(t).sort();
throw TypeError(
`target[${q(
method,
)}] is not a function, typeof is ${ftype}, has ${names}`,
`invoked method ${q(method)} is not a function, it is a ${ftype}`,
);
}
return t[method](...args);
Expand Down
35 changes: 35 additions & 0 deletions packages/eventual-send/test/test-e.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,41 @@ test('E method calls', async t => {
t.is(await d, 12, 'method call works');
});

test('E call missing method', async t => {
const x = {
double(n) {
return 2 * n;
},
};
await t.throwsAsync(() => E(x).triple(6), {
message: 'target has no method "triple", has [double]',
});
});

test('E call undefined method', async t => {
const x = {
double(n) {
return 2 * n;
},
};
await t.throwsAsync(() => E(x)(6), {
message: 'Cannot invoke target as a function, the type is object',
});
});

test('E invoke a non-method', async t => {
const x = { double: 24 };
await t.throwsAsync(() => E(x).double(6), {
message: 'invoked method "double" is not a function, it is a number',
});
});

test('E method call undefined receiver', async t => {
await t.throwsAsync(() => E(undefined).double(6), {
message: 'Cannot deliver "double" to target; typeof target is "undefined"',
});
});

test('E shortcuts', async t => {
const x = {
name: 'buddy',
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ test(`zcf.saveIssuer - bad issuer`, async t => {
await t.throwsAsync(() => zcf.saveIssuer(moolaKit.brand, 'A'), {
// TODO: improve error message
// https://github.com/Agoric/agoric-sdk/issues/1701
message: /^target\["getBrand"\] does not exist/,
message: 'target has no method "getBrand", has [getAllegedName,isMyIssuer]',
});
});

Expand Down

0 comments on commit f33d30e

Please sign in to comment.