Skip to content

Commit

Permalink
Improve “Illegal invocation” error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed May 3, 2020
1 parent 7d45dfc commit c8a6db2
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 342 deletions.
8 changes: 5 additions & 3 deletions lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Attribute {

let brandCheck = `
if (!exports.is(esValue)) {
throw new TypeError("Illegal invocation");
throw new TypeError("'$KEYWORD$ ${this.idl.name}' called on an object that does not implement interface ${this.interface.name}.");
}
`;
let getterBody = `return utils.tryWrapperForImpl(esValue[implSymbol]["${this.idl.name}"]);`;
Expand Down Expand Up @@ -78,11 +78,13 @@ class Attribute {
addMethod(this.idl.name, [], `
${promiseHandlingBefore}
const esValue = this !== null && this !== undefined ? this : globalObject;
${brandCheck}
${brandCheck.replace("$KEYWORD$", "get")}
${getterBody}
${promiseHandlingAfter}
`, "get", { configurable });

brandCheck = brandCheck.replace("$KEYWORD$", "set");

if (!this.idl.readonly) {
if (async) {
throw new Error(`Illegal promise-typed attribute "${this.idl.name}" in interface "${this.interface.idl.name}"`);
Expand Down Expand Up @@ -159,7 +161,7 @@ class Attribute {
addMethod("toString", [], `
const esValue = this;
if (!exports.is(esValue)) {
throw new TypeError("Illegal invocation");
throw new TypeError("'toString' called on an object that does not implement interface ${this.interface.name}.");
}
${getterBody}
Expand Down
8 changes: 4 additions & 4 deletions lib/constructs/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Iterable {

generateFunction(key, kind) {
this.interface.addMethod(this.interface.defaultWhence, key, [], `
if (!this || !exports.is(this)) {
throw new TypeError("Illegal invocation");
if (!exports.is(this)) {
throw new TypeError("'${key}' called on an object that does not implement interface ${this.interface.name}.");
}
return exports.createDefaultIterator(this, "${kind}");
`);
Expand All @@ -35,8 +35,8 @@ class Iterable {
this.generateFunction("entries", "key+value");
this.interface.addProperty(whence, Symbol.iterator, `${this.interface.name}.prototype.entries`);
this.interface.addMethod(whence, "forEach", ["callback"], `
if (!this || !exports.is(this)) {
throw new TypeError("Illegal invocation");
if (!exports.is(this)) {
throw new TypeError("'forEach' called on an object that does not implement interface ${this.interface.name}.");
}
if (arguments.length < 1) {
throw new TypeError("Failed to execute 'forEach' on '${this.name}': 1 argument required, " +
Expand Down
2 changes: 1 addition & 1 deletion lib/constructs/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Operation {
str += `
const esValue = this !== null && this !== undefined ? this : globalObject;
if (!exports.is(esValue)) {
throw new TypeError("Illegal invocation");
throw new TypeError("'${this.name}' called on an object that does not implement interface ${this.interface.name}.");
}
`;
}
Expand Down
Loading

0 comments on commit c8a6db2

Please sign in to comment.