Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #168 from charlespwd/master
Browse files Browse the repository at this point in the history
Throw slightly better error message when a context variable is not provided
  • Loading branch information
jasonmit authored Aug 8, 2017
2 parents 31e87c6 + 63c46fd commit 33f175c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};
}

Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down
4 changes: 4 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'/);
});
});

Expand All @@ -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}'/);
});
});

Expand All @@ -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}'/);
});
});

Expand All @@ -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}'/);
});
});

Expand Down

0 comments on commit 33f175c

Please sign in to comment.