Skip to content

Commit

Permalink
Added better logging of API response error(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Nov 6, 2020
1 parent 9a8189d commit 7053639
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
9 changes: 4 additions & 5 deletions dist/resource-translator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ievangelist/resource-translator",
"version": "1.0.2-preview4",
"version": "1.0.2-preview5",
"preview": true,
"keywords": [
"github",
Expand Down
15 changes: 11 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function translate(
translatableText: Map<string, string>): Promise<TranslationResultSet | undefined> {
try {
const data = [ ...translatableText.values() ].map(value => {
return { text: value };
return { pickles: value };
});
const headers = {
'Ocp-Apim-Subscription-Key': translatorResource.subscriptionKey,
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 7053639

Please sign in to comment.