Skip to content

Commit

Permalink
Merge pull request #44 from ttsukagoshi/handle-deepl-errors
Browse files Browse the repository at this point in the history
Add function to handle DeepL API errors
  • Loading branch information
ttsukagoshi authored Mar 22, 2023
2 parents f66b4b5 + 46d9557 commit b74a2ef
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions src/sheetsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ type DeepLTranslationObj = {
text: string;
};

/**
* The type of language that should be returned in the GET request
* to the DeepL API to retrieve its supported languages.
* @see https://www.deepl.com/docs-api/general/get-languages/
*/
type DeepLLanguageType = 'source' | 'target';

/**
* Create add-on menu on opening spreadsheet file.
*/
Expand Down Expand Up @@ -352,23 +359,12 @@ export function deepLTranslate(
}
// console.log(`url: ${url}`);

// Call the DeepL API translate request
const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
// Handle error codes
// See https://www.deepl.com/docs-api/api-access/error-handling/
const responseCode = response.getResponseCode();
if (responseCode === 429) {
throw new Error(
`[${ADDON_NAME}] Too Many Requests: Try again after some time.`
);
} else if (responseCode === 456) {
throw new Error(
`[${ADDON_NAME}] Quota Exceeded: The translation limit of your account has been reached.`
);
} else if (responseCode !== 200) {
throw new Error(
`[${ADDON_NAME}] Error on Calling DeepL API: ${response.getContentText()}`
);
}

// Handle DeepL API errors
handleDeepLErrors(response);

const translatedTextObj: DeepLTranslationResponse = JSON.parse(
response.getContentText()
);
Expand All @@ -384,11 +380,13 @@ export function deepLTranslate(
/**
* Retrieve the list of languages that are currently supported for translation,
* either as source or target language.
* @param type Sets whether source or target languages should be listed. Takes either `source` or `target`.
* @param type The type of languages that should be listed.
* @returns An array of the supported languages.
* @see https://www.deepl.com/docs-api/general/get-languages/
*/
export function deepLGetLanguages(type = 'source'): DeepLSupportedLanguages[] {
export function deepLGetLanguages(
type: DeepLLanguageType = 'source'
): DeepLSupportedLanguages[] {
const endpoint = 'languages';
// API key
const apiKey =
Expand All @@ -402,11 +400,11 @@ export function deepLGetLanguages(type = 'source'): DeepLSupportedLanguages[] {
// Call the DeepL API
let url = baseUrl + endpoint + `?auth_key=${apiKey}&type=${type}`;
const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
if (response.getResponseCode() === 200) {
return JSON.parse(response.getContentText());
} else {
throw new Error(`[${ADDON_NAME}] ${response.getContentText()}`);
}

// Handle DeepL API errors
handleDeepLErrors(response);

return JSON.parse(response.getContentText());
}

/**
Expand All @@ -430,3 +428,31 @@ export function getDeepLApiBaseUrl(apiKey: string): string {
export function getBlobBytes(text: string): number {
return Utilities.newBlob(text).getBytes().length;
}

/**
* Handle DeepL API errors based on the response code.
* @param response The UrlFetchApp.fetch response from the DeepL API
* @see https://www.deepl.com/docs-api/api-access/error-handling/
*/
export function handleDeepLErrors(
response: GoogleAppsScript.URL_Fetch.HTTPResponse
): void {
const responseCode = response.getResponseCode();
if (responseCode === 429) {
throw new Error(
`[${ADDON_NAME}] Too Many Requests: Try again after some time.`
);
} else if (responseCode === 456) {
throw new Error(
`[${ADDON_NAME}] Quota Exceeded: The translation limit of your account has been reached.`
);
} else if (responseCode >= 500) {
throw new Error(
`[${ADDON_NAME}] Temporary errors in the DeepL service. Please retry after waiting for a while.`
);
} else if (responseCode !== 200) {
throw new Error(
`[${ADDON_NAME}] Error on Calling DeepL API: ${response.getContentText()}`
);
}
}

0 comments on commit b74a2ef

Please sign in to comment.