Skip to content

Commit

Permalink
chore: Refactor translator.js to improve error handling and retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhou26 committed Jun 17, 2024
1 parent 70fc070 commit 8ba45fd
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ async function translate(text, sourceLang = 'AUTO', targetLang = 'ZH', numberAlt
const proxy = getNextProxy();
const cookie = getNextCookie();

if (!proxy || !cookie) {
// Add a maximum retry limit
const maxRetries = 5;
if (tryCount >= maxRetries) {
console.error("Max retry limit reached.");
return null;
}

if (!proxy && !cookie) {
console.error("No valid proxies or cookies available.");
return null;
}

const headers = { ...baseHeaders, 'proxy': proxy, 'cookie': cookie };
const headers = { ...baseHeaders };
if (proxy) headers['proxy'] = proxy;
if (cookie) headers['cookie'] = cookie;

const postData = {
jsonrpc: '2.0',
Expand All @@ -72,8 +81,11 @@ async function translate(text, sourceLang = 'AUTO', targetLang = 'ZH', numberAlt
return response.data.result.texts[0];
} catch (err) {
console.error("response error:" + err);
markProxyInvalid(proxy);
markCookieInvalid(cookie);

// Mark the proxy and/or cookie as invalid only if they were used
if (proxy) markProxyInvalid(proxy);
if (cookie) markCookieInvalid(cookie);

console.log("Trying again due to assuming the current proxy or cookie is invalid...");
return await translate(text, sourceLang, targetLang, numberAlternative, tryCount + 1);
}
Expand Down

0 comments on commit 8ba45fd

Please sign in to comment.