1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Tolerate missing or falsey substitutions (#8907)

Previously the `getMessage` function would throw if a substitution was
falsey. Now it will accept any substitution, including `undefined`.

A substitution of `null` or `undefined` will still be reported to
Sentry and printed to the console as an error, but it will not
interrupt execution. Any `null` or `undefined` substitutions will be
rendered as empty strings.

Ideally we'd never pass in `null` or `undefined` as a substitution, but
in practice this sometimes just occurs breifly between renders, which
isn't a severe enough problem to justify crashing the UI.

The detection of React component substitutions has been updated as
well, to ensure that `null` values aren't counted as React
substitutions.
This commit is contained in:
Mark Stacey 2020-07-03 13:02:35 -03:00 committed by GitHub
parent 06eaeef12c
commit 564f76584b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 12 deletions

View File

@ -43,7 +43,7 @@ export const getMessage = (localeCode, localeMessages, key, substitutions) => {
const hasSubstitutions = Boolean(substitutions && substitutions.length) const hasSubstitutions = Boolean(substitutions && substitutions.length)
const hasReactSubstitutions = hasSubstitutions && const hasReactSubstitutions = hasSubstitutions &&
substitutions.some((element) => typeof element === 'function' || typeof element === 'object') substitutions.some((element) => element !== null && (typeof element === 'function' || typeof element === 'object'))
// perform substitutions // perform substitutions
if (hasSubstitutions) { if (hasSubstitutions) {
@ -55,10 +55,12 @@ export const getMessage = (localeCode, localeMessages, key, substitutions) => {
return part return part
} }
const substituteIndex = Number(subMatch[1]) - 1 const substituteIndex = Number(subMatch[1]) - 1
if (substitutions[substituteIndex]) { if (substitutions[substituteIndex] == null) {
return substitutions[substituteIndex] const error = new Error(`Insufficient number of substitutions for message: '${phrase}'`)
log.error(error)
Sentry.captureException(error)
} }
throw new Error(`Insufficient number of substitutions for message: '${phrase}'`) return substitutions[substituteIndex]
}) })
phrase = hasReactSubstitutions phrase = hasReactSubstitutions

View File

@ -132,14 +132,14 @@ describe('i18n helper', function () {
assert.equal(result, `${TEST_SUBSTITUTION_1} - ${TEST_SUBSTITUTION_2} - ${TEST_SUBSTITUTION_3} - ${TEST_SUBSTITUTION_4} - ${TEST_SUBSTITUTION_5}`) assert.equal(result, `${TEST_SUBSTITUTION_1} - ${TEST_SUBSTITUTION_2} - ${TEST_SUBSTITUTION_3} - ${TEST_SUBSTITUTION_4} - ${TEST_SUBSTITUTION_5}`)
}) })
it('should throw an error when not passed as many substitutions as a message requires', function () { it('should correctly render falsey substitutions', function () {
assert.throws( const result = t(TEST_KEY_4, [ 0, -0, '', false, NaN ])
() => { assert.equal(result, '0 - 0 - - false - NaN')
t(TEST_KEY_5, [ TEST_SUBSTITUTION_1, TEST_SUBSTITUTION_2 ]) })
},
Error, it('should render nothing for "null" and "undefined" substitutions', function () {
`Insufficient number of substitutions for message: '$1 - $2 - $3'` const result = t(TEST_KEY_5, [ null, TEST_SUBSTITUTION_2 ])
) assert.equal(result, ` - ${TEST_SUBSTITUTION_2} - `)
}) })
it('should return the correct message when a single react substitution is made', function () { it('should return the correct message when a single react substitution is made', function () {