Skip to content

Commit

Permalink
Add error context
Browse files Browse the repository at this point in the history
Also eliminate outdated dictionary error for Date and RegExp.
  • Loading branch information
TimothyGu committed May 2, 2017
1 parent 12b95ee commit 0b23e56
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Attribute.prototype.generate = function () {
${getterBody}
},`;
if (!this.idl.readonly) {
const conv = Types.generateTypeConversion(this.ctx, "V", this.idl.idlType, this.idl.extAttrs, this.interface.name);
const conv = Types.generateTypeConversion(this.ctx, "V", this.idl.idlType, this.idl.extAttrs, this.interface.name, `"Failed to set the '${this.idl.name}' property on '${this.interface.name}': The provided value"`);
Object.assign(requires, conv.requires);
let conversion = conv.body.replace(/\n/g, "\n ");
str += `
Expand Down
15 changes: 6 additions & 9 deletions lib/constructs/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Dictionary {
if (value !== undefined) {`;
const argAttrs = field.extAttrs;

const conv = Types.generateTypeConversion(this.ctx, "value", typeConversion, argAttrs, this.name);
const conv = Types.generateTypeConversion(this.ctx, "value", typeConversion, argAttrs, this.name, `\`\${context} has member ${field.name} that\``);
for (let key in conv.requires) {
this.str = `const ${key} = ${conv.requires[key]};\n` + this.str;
}
Expand Down Expand Up @@ -63,26 +63,23 @@ class Dictionary {
generate() {
this.str += `
module.exports = {
convertInherit(obj, ret) {`;
convertInherit(obj, ret, { context = "The provided value" } = {}) {`;
if (this.idl.inheritance) {
this.str = `const ${this.idl.inheritance} = require("./${this.idl.inheritance}");\n` + this.str;
this.str += `
${this.idl.inheritance}.convertInherit(obj, ret);`;
${this.idl.inheritance}.convertInherit(obj, ret, { context });`;
}
this._generateConversions();
this.str += `
},
convert(obj) {
convert(obj, { context = "The provided value" } = {}) {
if (obj !== undefined && typeof obj !== "object") {
throw new TypeError("Dictionary has to be an object");
}
if (obj instanceof Date || obj instanceof RegExp) {
throw new TypeError("Dictionary may not be a Date or RegExp object");
throw new TypeError(\`\${context} is not an object.\`);
}
const ret = Object.create(null);
module.exports.convertInherit(obj, ret);
module.exports.convertInherit(obj, ret, { context });
return ret;
}
};`;
Expand Down
6 changes: 3 additions & 3 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Interface.prototype.generateConstructor = function () {
}
}

const conversions = Parameters.generateOverloadConversions(this.ctx, overloads, this.name);
const conversions = Parameters.generateOverloadConversions(this.ctx, overloads, this.name, `Failed to construct '${this.name}': `);
Object.assign(this.requires, conversions.requires);

minConstructor.nameList = minConstructor.nameList.map((name) => (keywords.has(name) ? "_" : "") + name);
Expand Down Expand Up @@ -187,11 +187,11 @@ Interface.prototype.generateExport = function () {
}
return false;
},
convert(obj) {
convert(obj, { context = "The provided value" } = {}) {
if (module.exports.is(obj)) {
return utils.implForWrapper(obj);
}
throw new TypeError("The value provided is not a ${this.name}");
throw new TypeError(\`\${context} is not of type '${this.name}'.\`);
},`;

if (this.iterable) {
Expand Down
2 changes: 1 addition & 1 deletion lib/constructs/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Operation.prototype.generate = function () {

const callOn = this.idl.static ? "Impl" : "this[impl]";

const parameterConversions = Parameters.generateOverloadConversions(this.ctx, overloads, this.interface.name);
const parameterConversions = Parameters.generateOverloadConversions(this.ctx, overloads, this.interface.name, `Failed to execute '${name}' on '${this.obj.name}': `);
const argsSpread = parameterConversions.hasArgs ? "...args" : "";
Object.assign(requires, parameterConversions.requires);
str += parameterConversions.body;
Expand Down
8 changes: 4 additions & 4 deletions lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getDefault(dflt) {
throw new Error("Unexpected default type: " + dflt.type);
}

function generateVarConversion(ctx, name, conversion, argAttrs, parentName) {
function generateVarConversion(ctx, name, conversion, argAttrs, ...typeArgs) {
const { customTypes } = ctx;
const requires = {};
let str = "";
Expand All @@ -33,7 +33,7 @@ function generateVarConversion(ctx, name, conversion, argAttrs, parentName) {
if (${name} !== undefined) {`;
}

const conv = Types.generateTypeConversion(ctx, name, idlType, argAttrs, parentName);
const conv = Types.generateTypeConversion(ctx, name, idlType, argAttrs, ...typeArgs);
Object.assign(requires, conv.requires);
str += conv.body;

Expand All @@ -53,7 +53,7 @@ if (${name} !== undefined) {`;
};
};

module.exports.generateOverloadConversions = function (ctx, overloads, parentName) {
module.exports.generateOverloadConversions = function (ctx, overloads, parentName, errPrefix) {
const { customTypes } = ctx;
const requires = {};
let str = ``;
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports.generateOverloadConversions = function (ctx, overloads, parentNam
continue;
}

const conv = generateVarConversion(ctx, `args[${i}]`, typeConversions[i], maxConstructor.operation.arguments[i].extAttrs, parentName);
const conv = generateVarConversion(ctx, `args[${i}]`, typeConversions[i], maxConstructor.operation.arguments[i].extAttrs, parentName, `"${errPrefix}parameter ${i + 1}"`);
Object.assign(requires, conv.requires);
str += conv.body;
}
Expand Down
21 changes: 11 additions & 10 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const conversions = require("webidl-conversions");

const utils = require("./utils");

function generateTypeConversion(ctx, name, idlType, argAttrs, parentName) {
function generateTypeConversion(ctx, name, idlType, argAttrs, parentName, errPrefix = '"The provided value"') {
const { customTypes } = ctx;
const requires = {};
let str = "";
Expand Down Expand Up @@ -62,13 +62,13 @@ function generateTypeConversion(ctx, name, idlType, argAttrs, parentName) {
function generateSequence() {
str += `
if (typeof ${name} !== "object") {
throw new TypeError("The value provided is not an iterable object");
throw new TypeError(${errPrefix} + " is not an iterable object.");
} else {
const V = [];
const tmp = ${name};
for (let nextItem of tmp) {`;

const conv = generateTypeConversion(ctx, "nextItem", idlType.idlType, [], parentName);
const conv = generateTypeConversion(ctx, "nextItem", idlType.idlType, [], parentName, errPrefix);
Object.assign(requires, conv.requires);
str += conv.body;

Expand All @@ -89,7 +89,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs, parentName) {

str += `
if (typeof ${name} !== "object") {
throw new TypeError("The value provided is not an object");
throw new TypeError(${errPrefix} + " is not an object.");
} else {
const result = Object.create(null);
const keys = Object.getOwnPropertyNames(${name});
Expand All @@ -100,9 +100,9 @@ function generateTypeConversion(ctx, name, idlType, argAttrs, parentName) {
let typedKey = key;
let typedValue = value;`;

str += generateTypeConversion(ctx, "typedKey", idlType.idlType[0], [], parentName).body;
str += generateTypeConversion(ctx, "typedKey", idlType.idlType[0], [], parentName, errPrefix).body;

const conv = generateTypeConversion(ctx, "typedValue", idlType.idlType[1], [], parentName);
const conv = generateTypeConversion(ctx, "typedValue", idlType.idlType[1], [], parentName, errPrefix);
Object.assign(requires, conv.requires);
str += conv.body;

Expand Down Expand Up @@ -135,14 +135,15 @@ function generateTypeConversion(ctx, name, idlType, argAttrs, parentName) {
const clamp = utils.getExtAttr(argAttrs, "Clamp");
const treatNullAs = utils.getExtAttr(argAttrs, "TreatNullAs");

let optString = "";
let optString = `, { context: ${errPrefix}`;
if (clamp) {
optString = `, { clamp: true }`;
optString += ", clamp: true";
} else if (enforceRange) {
optString = `, { enforceRange: true }`;
optString += ", enforceRange: true";
} else if (treatNullAs && treatNullAs.rhs.value === "EmptyString") {
optString = `, { treatNullAsEmptyString: true }`;
optString += ", treatNullAsEmptyString: true";
}
optString += " }";
if (idlType.array) {
str += `
for (let i = 0; i < ${name}.length; ++i) {
Expand Down

0 comments on commit 0b23e56

Please sign in to comment.