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 authored May 10, 2020
1 parent 696fc22 commit 21a948e
Show file tree
Hide file tree
Showing 4 changed files with 436 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 is not a valid instance of ${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 is not a valid instance of ${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 is not a valid instance of ${this.interface.name}.");
}
return exports.createDefaultIterator(this, "${kind}");
`);
Expand All @@ -37,8 +37,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 is not a valid instance of ${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 @@ -107,7 +107,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 is not a valid instance of ${this.interface.name}.");
}
`;
}
Expand Down
Loading

0 comments on commit 21a948e

Please sign in to comment.