Skip to content

Commit

Permalink
fix: suppress unhandled promise rejection errors (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradmiro authored and Ace Nassri committed Nov 17, 2022
1 parent cd8ff0b commit a7c7d36
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 114 deletions.
17 changes: 10 additions & 7 deletions translate/v3/translate_batch_translate_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ function main(
},
};

// Batch translate text using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.batchTranslateText(request);
try {
// Batch translate text using a long-running operation
const [operation] = await translationClient.batchTranslateText(request);

// Wait for operation to complete.
const [response] = await operation.promise();
// Wait for operation to complete.
const [response] = await operation.promise();

console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
} catch (error) {
console.error(error.details);
}
}

batchTranslateText();
Expand Down
18 changes: 11 additions & 7 deletions translate/v3/translate_batch_translate_text_with_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ function main(
},
};

// Create a job whose results you can either wait for now, or get later
const [operation] = await client.batchTranslateText(request);
try {
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request);

// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
// Wait for the operation to complete
const [response] = await operation.promise();

// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
} catch (error) {
console.error(error.details);
}
}

batchTranslateTextWithGlossary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ function main(
},
};

// Create a job whose results you can either wait for now, or get later
const [operation] = await client.batchTranslateText(request);
try {
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request);

// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
// Wait for operation to complete
const [response] = await operation.promise();

// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
} catch (error) {
console.error(error.details);
}
}

batchTranslateTextWithGlossaryAndModel();
Expand Down
18 changes: 11 additions & 7 deletions translate/v3/translate_batch_translate_text_with_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ function main(
},
};

// Create a job whose results you can either wait for now, or get later
const [operation] = await client.batchTranslateText(request);
try {
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request);

// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
// Wait for the operation to complete
const [response] = await operation.promise();

// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
} catch (error) {
console.error(error.details);
}
}

batchTranslateTextWithModel();
Expand Down
5 changes: 2 additions & 3 deletions translate/v3/translate_create_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ function main(
glossary: glossary,
};

// Create glossary using a long-running operation
// and wait for its completion
try {
// Create glossary using a long-running operation
const [operation] = await translationClient.createGlossary(request);

// Wait for operation to complete.
// Wait for the operation to complete
await operation.promise();

console.log(`Created glossary:`);
Expand Down
15 changes: 9 additions & 6 deletions translate/v3/translate_delete_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ function main(
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// Delete glossary using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.deleteGlossary(request);
try {
// Delete glossary using a long-running operation
const [operation] = await translationClient.deleteGlossary(request);

// Wait for operation to complete.
const [response] = await operation.promise();
// Wait for operation to complete.
const [response] = await operation.promise();

console.log(`Deleted glossary: ${response.name}`);
console.log(`Deleted glossary: ${response.name}`);
} catch (error) {
console.error(error.details);
}
}

deleteGlossary();
Expand Down
18 changes: 11 additions & 7 deletions translate/v3/translate_detect_language.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ function main(
content: text,
};

// Run request
const [response] = await translationClient.detectLanguage(request);

console.log(`Detected Languages:`);
for (const language of response.languages) {
console.log(`Language Code: ${language.languageCode}`);
console.log(`Confidence: ${language.confidence}`);
try {
// Run request
const [response] = await translationClient.detectLanguage(request);

console.log(`Detected Languages:`);
for (const language of response.languages) {
console.log(`Language Code: ${language.languageCode}`);
console.log(`Confidence: ${language.confidence}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
12 changes: 8 additions & 4 deletions translate/v3/translate_get_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ function main(
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// Get glossary
const [response] = await translationClient.getGlossary(request);

console.log(`Got glossary: ${response.name}`);
try {
// Get glossary
const [response] = await translationClient.getGlossary(request);

console.log(`Got glossary: ${response.name}`);
} catch (error) {
console.error(error.details);
}
}

getGlossary();
Expand Down
31 changes: 19 additions & 12 deletions translate/v3/translate_get_supported_languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
parent: `projects/${projectId}/locations/${location}`,
};

// Get supported languages
const [response] = await translationClient.getSupportedLanguages(request);

for (const language of response.languages) {
// Supported language code, generally consisting of its ISO 639-1 identifier, for
// example, 'en', 'ja'. In certain cases, BCP-47 codes including language and
// region identifiers are returned (for example, 'zh-TW' and 'zh-CN')
console.log(`Language - Language Code: ${language.languageCode}`);
// Can be used as source language.
console.log(`Language - Support Source: ${language.supportSource}`);
// Can be used as target language.
console.log(`Language - Support Target: ${language.supportTarget}`);
try {
// Get supported languages
const [response] = await translationClient.getSupportedLanguages(request);

for (const language of response.languages) {
// Supported language code, generally consisting of its ISO 639-1 identifier, for
// example, 'en', 'ja'. In certain cases, BCP-47 codes including language and
// region identifiers are returned (for example, 'zh-TW' and 'zh-CN')
console.log(`Language - Language Code: ${language.languageCode}`);
// Human readable name of the language localized in the display language specified
// in the request.
console.log(`Language - Display Name: ${language.displayName}`);
// Can be used as source language.
console.log(`Language - Support Source: ${language.supportSource}`);
// Can be used as target language.
console.log(`Language - Support Target: ${language.supportTarget}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
34 changes: 19 additions & 15 deletions translate/v3/translate_get_supported_languages_for_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,25 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
displayLanguageCode: 'en',
};

// Get supported languages
const [response] = await translationClient.getSupportedLanguages(request);

for (const language of response.languages) {
// Supported language code, generally consisting of its ISO 639-1 identifier, for
// example, 'en', 'ja'. In certain cases, BCP-47 codes including language and
// region identifiers are returned (for example, 'zh-TW' and 'zh-CN')
console.log(`Language - Language Code: ${language.languageCode}`);
// Human readable name of the language localized in the display language specified
// in the request.
console.log(`Language - Display Name: ${language.displayName}`);
// Can be used as source language.
console.log(`Language - Support Source: ${language.supportSource}`);
// Can be used as target language.
console.log(`Language - Support Target: ${language.supportTarget}`);
try {
// Get supported languages
const [response] = await translationClient.getSupportedLanguages(request);

for (const language of response.languages) {
// Supported language code, generally consisting of its ISO 639-1 identifier, for
// example, 'en', 'ja'. In certain cases, BCP-47 codes including language and
// region identifiers are returned (for example, 'zh-TW' and 'zh-CN')
console.log(`Language - Language Code: ${language.languageCode}`);
// Human readable name of the language localized in the display language specified
// in the request.
console.log(`Language - Display Name: ${language.displayName}`);
// Can be used as source language.
console.log(`Language - Support Source: ${language.supportSource}`);
// Can be used as target language.
console.log(`Language - Support Target: ${language.supportTarget}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
16 changes: 10 additions & 6 deletions translate/v3/translate_list_codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
parent: `projects/${projectId}/locations/${location}`,
};

// Run request
const [response] = await translationClient.getSupportedLanguages(request);

console.log(`Supported languages:`);
for (const language of response.languages) {
console.log(`Language Code: ${language.languageCode}`);
try {
// Run request
const [response] = await translationClient.getSupportedLanguages(request);

console.log(`Supported languages:`);
for (const language of response.languages) {
console.log(`Language Code: ${language.languageCode}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
22 changes: 13 additions & 9 deletions translate/v3/translate_list_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') {
parent: `projects/${projectId}/locations/${location}`,
};

// Run request
const [response] = await translationClient.listGlossaries(request);

for (const glossary of response) {
console.log(`Name: ${glossary.name}`);
console.log(`Entry count: ${glossary.entryCount}`);
console.log(`Input uri: ${glossary.inputConfig.gcsSource.inputUri}`);
for (const languageCode of glossary.languageCodesSet.languageCodes) {
console.log(`Language code: ${languageCode}`);
try {
// Run request
const [response] = await translationClient.listGlossaries(request);

for (const glossary of response) {
console.log(`Name: ${glossary.name}`);
console.log(`Entry count: ${glossary.entryCount}`);
console.log(`Input uri: ${glossary.inputConfig.gcsSource.inputUri}`);
for (const languageCode of glossary.languageCodesSet.languageCodes) {
console.log(`Language code: ${languageCode}`);
}
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
18 changes: 11 additions & 7 deletions translate/v3/translate_list_language_names.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
displayLanguageCode: 'fr',
};

// Run request
const [response] = await translationClient.getSupportedLanguages(request);

console.log(`Supported languages:`);
for (const language of response.languages) {
console.log(`Language Code: ${language.languageCode}`);
console.log(`Display Name: ${language.displayName}`);
try {
// Run request
const [response] = await translationClient.getSupportedLanguages(request);

console.log(`Supported languages:`);
for (const language of response.languages) {
console.log(`Language Code: ${language.languageCode}`);
console.log(`Display Name: ${language.displayName}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
14 changes: 9 additions & 5 deletions translate/v3/translate_translate_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ function main(
targetLanguageCode: 'sr-Latn',
};

// Run request
const [response] = await translationClient.translateText(request);

for (const translation of response.translations) {
console.log(`Translation: ${translation.translatedText}`);
try {
// Run request
const [response] = await translationClient.translateText(request);

for (const translation of response.translations) {
console.log(`Translation: ${translation.translatedText}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
12 changes: 8 additions & 4 deletions translate/v3/translate_translate_text_with_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ function main(
glossaryConfig: glossaryConfig,
};

// Run request
const [response] = await translationClient.translateText(request);
try {
// Run request
const [response] = await translationClient.translateText(request);

for (const translation of response.glossaryTranslations) {
console.log(`Translation: ${translation.translatedText}`);
for (const translation of response.glossaryTranslations) {
console.log(`Translation: ${translation.translatedText}`);
}
} catch (error) {
console.error(error.details);
}
}

Expand Down
Loading

0 comments on commit a7c7d36

Please sign in to comment.