Defend against various internal errors with object notation #129
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These changes simply allow us to bail out of the whole method when an inappropriate input is detected. Otherwise, the functions might cause errors like
Cannot call method 'hasOwnProperty' of undefined
.The above case can happen when, for example, there's a syntax error in a locale .json file.
Returning a noop when the locale wasn't found, will ultimately result in the locale being written. So, if there was an error in the JSON file (trailing comma, string not closed, …), the whole file will be overwritten. Personally, I consider this a benefit, because it indicates to me that there was a problem with the locale file. If you have no copies of the file (in VCS or wherever), it would probably suck.
To prevent all of this happening, it might be wise to check
locales[locale]
after it was read (This is already happening, but the final read ofdefaultLocale
is never checked). For reference: https://github.com/oliversalzburg/i18n-node/blob/0711eb7591c6b961cb1bc927ae1dcbf148b29752/i18n.js#L438