From 7e0ea9d52de5c5d5cf588d498af1f9d8acbbc5ef Mon Sep 17 00:00:00 2001 From: Khaled Mohamed Date: Thu, 29 Jun 2017 00:41:21 -0400 Subject: [PATCH] fix(google-translate): Google failed when we translate empty string array, skip empty string or arra --- __mocks__/yandex-translate.js | 2 +- __tests__/api/general.spec.js | 8 ++++++++ lib/service/google.js | 16 ++++++++++++++-- lib/service/yandex.js | 8 ++++---- lib/translate-json-object.js | 6 +++++- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/__mocks__/yandex-translate.js b/__mocks__/yandex-translate.js index 6e423ed..684b0c6 100644 --- a/__mocks__/yandex-translate.js +++ b/__mocks__/yandex-translate.js @@ -3,7 +3,7 @@ module.exports = function () { translate: (value, lang, fn) => { if (value) { var translatedList = value + '-' + lang.to; - fn(null, {text: [translatedList]}); + fn(null, {code: 200, text: [translatedList]}); } else { fn({error: 'provide value'}); } diff --git a/__tests__/api/general.spec.js b/__tests__/api/general.spec.js index 8ce2cc3..7cb7aeb 100644 --- a/__tests__/api/general.spec.js +++ b/__tests__/api/general.spec.js @@ -39,6 +39,14 @@ describe('Translate Module TJO: ', () => { }); }); + it('Should fail to translate because of a missing language param: fail', () => { + init({yandexApiKey: 'apiToken'}); + return translate(mockDataObject) + .catch(err => { + expect(err).toBeDefined(); + }); + }); + it('should TJO.translate fails, becuase of missing google/yandex token', () => { init(); return translate(mockDataObject, 'ar') diff --git a/lib/service/google.js b/lib/service/google.js index 6341680..2fefa1a 100644 --- a/lib/service/google.js +++ b/lib/service/google.js @@ -14,9 +14,15 @@ function translateObject(language, key, destObj) { var valuesArray = _.concat(_.values(objKeys)); return new Promise(function (resolve, reject) { + // If empty array don't call google + if (valuesArray.length === 0) { + resolve(destObj); + return; + } + translateService.translate(valuesArray, language, function (err, res) { if (err || !res) { - reject('Translation service failed', err); + reject(err); } else { // Google-translate doesn't return an array if we only send a single string to translate res = _.concat(res); @@ -32,9 +38,15 @@ function translateObject(language, key, destObj) { function translateString(language, key, destObj, valueStr) { return new Promise(function (resolve, reject) { + // If empty string don't call google + if (valueStr === '') { + resolve(destObj); + return; + } + translateService.translate(valueStr, language, function (err, res) { if (err || !res) { - reject('Google translation service failed', err); + reject(err); } else { destObj[key] = res.translatedText; resolve(destObj); diff --git a/lib/service/yandex.js b/lib/service/yandex.js index 473c46f..11fb825 100644 --- a/lib/service/yandex.js +++ b/lib/service/yandex.js @@ -22,8 +22,8 @@ function translateObject(language, key, destObj) { return; } translateService.translate(valuesStr, {to: language}, function (err, res) { - if (err || !res) { - reject('Yandex translation service failed', err); + if (err || res.code !== 200) { + reject(err || res); } else { // yandex return an array string and so we need split back to the normal vlues var result = res.text[0].split(hashSimple); @@ -46,8 +46,8 @@ function translateString(language, key, destObj, valueStr) { } translateService.translate(valueStr, {to: language}, function (err, res) { - if (err || !res) { - reject('Yandex translation service failed', err); + if (err || res.code !== 200) { + reject(err || res); } else { // yandex return an array string and so we need split back to the normal vlues destObj[key] = res.text[0]; diff --git a/lib/translate-json-object.js b/lib/translate-json-object.js index aaa421b..1237f5c 100644 --- a/lib/translate-json-object.js +++ b/lib/translate-json-object.js @@ -50,6 +50,10 @@ function TranslateJSONObject() { ); } + if (_.isString(language)) { + return Promise.reject('Please provide a language param [type String] e.g. translate(obj, es)'); + } + function recurisveTranslateObject(destObj, srcObj) { // Loop through the entire object collection _.forEach(srcObj, loopHandler); @@ -101,7 +105,7 @@ function TranslateJSONObject() { Promise.all(promises).then(function () { resolve(destObj.data); }).catch(function (err) { - reject('Service failed', err); + reject(err); }); }); }