From 70536394add19e8c767eece806ba134f57f276c8 Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 5 Nov 2020 21:44:06 -0600 Subject: [PATCH] Added better logging of API response error(s) --- dist/resource-translator/index.js | 9 ++++----- package.json | 2 +- src/api.ts | 15 +++++++++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/dist/resource-translator/index.js b/dist/resource-translator/index.js index 1750f79..e59d2af 100644 --- a/dist/resource-translator/index.js +++ b/dist/resource-translator/index.js @@ -2124,7 +2124,6 @@ exports.translate = exports.getAvailableTranslations = void 0; const core_1 = __webpack_require__(470); const uuid_1 = __webpack_require__(898); const axios_1 = __importDefault(__webpack_require__(53)); -const utils_1 = __webpack_require__(163); async function getAvailableTranslations() { const url = 'https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation'; const response = await axios_1.default.get(url); @@ -2134,7 +2133,7 @@ exports.getAvailableTranslations = getAvailableTranslations; async function translate(translatorResource, toLocales, translatableText) { try { const data = [...translatableText.values()].map(value => { - return { text: value }; + return { pickles: value }; }); const headers = { 'Ocp-Apim-Subscription-Key': translatorResource.subscriptionKey, @@ -2176,9 +2175,9 @@ async function translate(translatorResource, toLocales, translatableText) { core_1.error(`Failed to translate input: ${JSON.stringify(ex)}`); // Try to write explicit error: // https://docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-reference#errors - const translatorError = utils_1.findValueByKey(ex, 'error'); - if (translatorError) { - core_1.error(`Error: ${JSON.stringify(translatorError)}`); + const e = ex.response.data; + if (e) { + core_1.error(`error: { code: ${e.error.code}, message: '${e.error.message}' }}`); } return undefined; } diff --git a/package.json b/package.json index bcaed79..7612208 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ievangelist/resource-translator", - "version": "1.0.2-preview4", + "version": "1.0.2-preview5", "preview": true, "keywords": [ "github", diff --git a/src/api.ts b/src/api.ts index eb93be2..c600446 100644 --- a/src/api.ts +++ b/src/api.ts @@ -18,7 +18,7 @@ export async function translate( translatableText: Map): Promise { try { const data = [ ...translatableText.values() ].map(value => { - return { text: value }; + return { pickles: value }; }); const headers = { 'Ocp-Apim-Subscription-Key': translatorResource.subscriptionKey, @@ -63,11 +63,18 @@ export async function translate( // Try to write explicit error: // https://docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-reference#errors - const translatorError = findValueByKey(ex, 'error'); - if (translatorError) { - error(`Error: ${JSON.stringify(translatorError)}`); + const e = ex.response.data as TranslationErrorResponse; + if (e) { + error(`error: { code: ${e.error.code}, message: '${e.error.message}' }}`); } return undefined; } +} + +interface TranslationErrorResponse { + error: { + code: number, + message: string; + } } \ No newline at end of file