diff --git a/src/core.js b/src/core.js index 7435f8d..2de7b8d 100644 --- a/src/core.js +++ b/src/core.js @@ -41,7 +41,18 @@ function MessageFormat(message, locales, formats) { // the other `Intl` APIs. var messageFormat = this; this.format = function (values) { + try { return messageFormat._format(pattern, values); + } catch (e) { + if (e.variableId) { + throw new Error( + 'The intl string context variable \'' + e.variableId + '\'' + + ' was not provided to the string \'' + message + '\'' + ); + } else { + throw e; + } + } }; } @@ -176,7 +187,7 @@ MessageFormat.prototype._findPluralRuleFunction = function (locale) { MessageFormat.prototype._format = function (pattern, values) { var result = '', - i, len, part, id, value; + i, len, part, id, value, err; for (i = 0, len = pattern.length; i < len; i += 1) { part = pattern[i]; @@ -191,7 +202,9 @@ MessageFormat.prototype._format = function (pattern, values) { // Enforce that all required values are provided by the caller. if (!(values && hop.call(values, id))) { - throw new Error('A value must be provided for: ' + id); + err = new Error('A value must be provided for: ' + id); + err.variableId = id; + throw err; } value = values[id]; diff --git a/tests/index.js b/tests/index.js index 3a347f5..7cf358c 100644 --- a/tests/index.js +++ b/tests/index.js @@ -317,6 +317,7 @@ describe('IntlMessageFormat', function () { it('should fail when the argument in the pattern is not provided', function () { expect(msg.format).to.throwException(function (e) { expect(e).to.be.an(Error); + expect(e.message).to.match(/The intl string context variable 'STATE' was not provided to the string '{STATE}'/); }); }); @@ -327,6 +328,7 @@ describe('IntlMessageFormat', function () { expect(formatWithValueNameTypo).to.throwException(function (e) { expect(e).to.be.an(Error); + expect(e.message).to.match(/The intl string context variable 'STATE' was not provided to the string '{STATE}'/); }); }); @@ -346,6 +348,7 @@ describe('IntlMessageFormat', function () { expect(formatWithMissingValue).to.throwException(function (e) { expect(e).to.be.an(Error); + expect(e.message).to.match(/The intl string context variable 'ST1ATE' was not provided to the string '{ST1ATE}'/); }); }); @@ -356,6 +359,7 @@ describe('IntlMessageFormat', function () { expect(formatWithMissingValue).to.throwException(function (e) { expect(e).to.be.an(Error); + expect(e.message).to.match(/The intl string context variable 'ST1ATE' was not provided to the string '{ST1ATE}'/); }); });