From 905f004b1511b4b399735e9659bca0538f297a43 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford <7019101+cwillisf@users.noreply.github.com> Date: Tue, 29 Oct 2024 08:57:19 -0700 Subject: [PATCH] fix: only retry the actual download Also, further improve error messaging --- lib/transifex.js | 35 ++++++++++++++++++++--------- scripts/tx-pull-www.mjs | 50 ++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/lib/transifex.js b/lib/transifex.js index ea93affb..b251eb08 100644 --- a/lib/transifex.js +++ b/lib/transifex.js @@ -103,19 +103,34 @@ const getResourceLocation = async function (projectSlug, resourceSlug, localeCod * strings, if the local is the source language) */ const txPull = async function (project, resource, locale, mode = 'default') { - const url = await getResourceLocation(project, resource, locale, mode); let buffer; - for (let i = 0; i < 5; i++) { - try { - buffer = await download(url); - buffer = buffer.toString(); - return JSON.parse(buffer); - } catch (e) { - console.error(e, {project, resource, locale, buffer}); - console.log(`Retrying after ${i + 1} failed attempt(s)`); + try { + const url = await getResourceLocation(project, resource, locale, mode); + for (let i = 0; i < 5; i++) { + if (i > 0) { + console.log(`Retrying txPull download after ${i} failed attempt(s)`); + } + try { + buffer = await download(url); // might throw(?) + break; + } catch (e) { + console.error(e, {project, resource, locale, buffer}); + } } + if (!buffer) { + throw Error(`txPull download failed after 5 retries: ${url}`); + } + buffer = buffer.toString(); + return JSON.parse(buffer); + } catch (e) { + e.cause = { + project, + resource, + locale, + buffer + }; + throw e; } - throw Error('failed to pull after 5 retries'); }; /** diff --git a/scripts/tx-pull-www.mjs b/scripts/tx-pull-www.mjs index ac3838fc..921bb37b 100755 --- a/scripts/tx-pull-www.mjs +++ b/scripts/tx-pull-www.mjs @@ -47,30 +47,34 @@ const getLocaleData = async function (item) { const locale = item.locale; const resource = item.resource; let txLocale = localeMap[locale] || locale; - for (let i = 0; i < 5; i++) { - try { - const translations = await txPull(PROJECT, resource, txLocale); - const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`; - mkdirp.sync(txOutdir); - const fileName = `${txOutdir}/${locale}.json`; + const translations = await txPull(PROJECT, resource, txLocale); - await fs.writeFile( - fileName, - JSON.stringify(translations, null, 4) - ); - this.progress.increment(); - return { - resource: resource, - locale: locale, - file: fileName - }; - } catch (e) { - console.error(e); - console.log(`retrying after ${i + 1} attempt(s)`); - } + const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`; + const fileName = `${txOutdir}/${locale}.json`; + + try { + mkdirp.sync(txOutdir); + await fs.writeFile( + fileName, + JSON.stringify(translations, null, 4) + ); + + return { + resource, + locale, + fileName + }; + } catch (e) { + e.cause = { + resource, + locale, + translations, + txOutdir, + fileName + }; + throw e; } - throw Error('failed to pull translations after 5 retries'); }; const expandResourceFiles = (resources) => { @@ -94,9 +98,9 @@ const pullTranslations = async function () { const progress = new ProgressLogger(allFiles.length); try { - await batchMap(allFiles, CONCURRENCY_LIMIT, item => { + await batchMap(allFiles, CONCURRENCY_LIMIT, async item => { try { - getLocaleData(item); + await getLocaleData(item); } finally { progress.increment(); }