Skip to content

Commit fe7eb82

Browse files
committed
fix: added try/catch for matching pair error
1 parent 904cc79 commit fe7eb82

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,29 @@ class Mandarin {
314314
// get the translation results from Google
315315
if (!_.isString(translation)) {
316316
debug('getting translation', key);
317-
[translation] = await this.client.translate(phrase, locale);
318-
debug('got translation', translation);
319-
if (this.redisClient) await this.redisClient.set(key, translation);
317+
try {
318+
[translation] = await this.client.translate(phrase, locale);
319+
} catch (err) {
320+
debug('error', err, 'key', key, 'phrase', phrase, 'locale', locale);
321+
}
322+
323+
if (_.isString(translation)) {
324+
debug('got translation', translation);
325+
if (this.redisClient) await this.redisClient.set(key, translation);
326+
}
320327
}
321328

322329
// replace `|` pipe character because translation will
323330
// interpret as ranged interval
324331
// <https://github.com/mashpie/i18n-node/issues/274>
325332
// TODO: maybe use `he` package to re-encode entities?
326-
file[phrase] = translation.replace(/\|/g, '&#124;');
333+
if (_.isString(translation)) {
334+
file[phrase] = translation.replace(/\|/g, '&#124;');
327335

328-
// write the file again
329-
debug('writing filePath', filePath, 'with translation', translation);
330-
await writeFile(filePath, JSON.stringify(file, null, 2));
336+
// write the file again
337+
debug('writing filePath', filePath, 'with translation', translation);
338+
await writeFile(filePath, JSON.stringify(file, null, 2));
339+
}
331340
});
332341

333342
return file;

0 commit comments

Comments
 (0)