From fbc43add9ad11107ca0fe87e92241be700dfa1ad Mon Sep 17 00:00:00 2001 From: Ben Peter Date: Wed, 25 Nov 2026 00:56:20 +0100 Subject: [PATCH 1/3] test(loc): cover page and project reads that throw A network error on a page read leaves the page without content or error in translate, and rejects the whole rollout, validate run or project load. --- test/loc/thrown-reads.test.js | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 test/loc/thrown-reads.test.js diff --git a/test/loc/thrown-reads.test.js b/test/loc/thrown-reads.test.js new file mode 100644 index 000000000..22490d434 --- /dev/null +++ b/test/loc/thrown-reads.test.js @@ -0,0 +1,85 @@ +import { expect } from '@esm-bundle/chai'; +import { rolloutLang } from '../../nx/blocks/loc/views/rollout/index.js'; +import { getUrls } from '../../nx/blocks/loc/views/translate/index.js'; +import { clearConfigCache, loadProject } from '../../nx/blocks/loc/utils/utils.js'; +import '../../nx/blocks/loc/views/validate/validate.js'; + +const message = 'Failed to fetch'; + +describe('loc reads that throw', () => { + let origFetch; + let calls; + + const failPageReads = () => { + calls = []; + window.fetch = async (url, opts = {}) => { + const href = url.toString(); + const method = opts.method || 'GET'; + calls.push({ href, method }); + if (href.endsWith('/.da/translate.json')) { + return new Response(JSON.stringify({ config: { data: [] } }), { status: 200 }); + } + if (method === 'GET') throw new TypeError(message); + return new Response('', { status: 200 }); + }; + }; + + beforeEach(() => { + origFetch = window.fetch; + clearConfigCache(); + }); + + afterEach(() => { + window.fetch = origFetch; + clearConfigCache(); + }); + + it('marks the page as failed in translate', async () => { + failPageReads(); + const { urls } = await getUrls('org', 'site', {}, '/en', '/en', [{ suppliedPath: '/en/page' }], true); + expect(urls[0].content).to.be.undefined; + expect(urls[0].error).to.equal(`Error fetching content from /en/page.html - ${message}`); + }); + + it('stops a rollout before writing anything', async () => { + failPageReads(); + const lang = { name: 'English', location: '/en', locales: [{ code: '/en/us' }], rollout: {} }; + const result = await rolloutLang({ + org: 'org', + site: 'site', + title: 'Proj', + options: { 'source.language': { location: '/en' }, 'rollout.conflict.behavior': 'overwrite' }, + lang, + urls: [{ suppliedPath: '/en/page' }], + actions: { requestUpdate: () => {} }, + }); + expect(result.aborted).to.equal(true); + expect(result.errors[0].error).to.include(message); + expect(calls.filter(({ method }) => method !== 'GET')).to.deep.equal([]); + }); + + it('marks the page as failed in validate', async () => { + failPageReads(); + const Validate = customElements.get('nx-loc-validate'); + const url = new URL('https://main--site--org.aem.page/en/page'); + const ctx = { + _org: 'org', + _site: 'site', + _urls: [url], + getOriginMatches: async () => [], + checkUrl: Validate.prototype.checkUrl, + findFragments: async () => {}, + requestUpdate: () => {}, + updateSourceMessage: async () => {}, + }; + await Validate.prototype.checkUrls.call(ctx); + expect(url.status).to.equal(`error - ${message}`); + expect(url.checked).to.equal(false); + }); + + it('reports an error when loading a project', async () => { + failPageReads(); + const result = await loadProject({ path: '/org/site/.da/translation/active/1', updateDocTitle: false }); + expect(result).to.deep.equal({ message: { text: message } }); + }); +}); From d38c8f72ccd2bc51f15904fea5db8b1420b9b073 Mon Sep 17 00:00:00 2001 From: Ben Peter Date: Wed, 25 Nov 2026 00:56:46 +0100 Subject: [PATCH 2/3] fix(loc): show an error when a page or project read throws Translate, rollout and validate mark the page with the error and carry on with the others, so translate doesn't send a page without content and rollout writes nothing. Loading a project shows the error instead of rejecting. --- nx/blocks/loc/utils/utils.js | 4 +++- nx/blocks/loc/views/rollout/index.js | 4 +++- nx/blocks/loc/views/translate/index.js | 6 ++++-- nx/blocks/loc/views/validate/validate.js | 6 +++++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/nx/blocks/loc/utils/utils.js b/nx/blocks/loc/utils/utils.js index 68937c218..7cb1d3743 100644 --- a/nx/blocks/loc/utils/utils.js +++ b/nx/blocks/loc/utils/utils.js @@ -459,7 +459,9 @@ async function fetchProject({ path, updates, updateDocTitle = true }) { opts.body = body; } - const resp = await daFetch({ url: `${DA_ADMIN}/source${path}.json`, opts }); + const resp = await daFetch({ url: `${DA_ADMIN}/source${path}.json`, opts }) + .catch((error) => error); + if (resp instanceof Error) return { message: { text: resp.message } }; if (!resp.ok) { if (resp.status === 401 || resp.status === 403) { const [org, site] = path.substring(1).split('/'); diff --git a/nx/blocks/loc/views/rollout/index.js b/nx/blocks/loc/views/rollout/index.js index 1244c4c6b..ac90f9af5 100644 --- a/nx/blocks/loc/views/rollout/index.js +++ b/nx/blocks/loc/views/rollout/index.js @@ -58,7 +58,9 @@ async function fetchLangSources(lang, urls) { return url; }; - const queue = new Queue(fetchUrl, MAX_CONCURRENT_READS); + const queue = new Queue(fetchUrl, MAX_CONCURRENT_READS, (url, error) => { + url.error = `Error fetching content from ${url.source} - ${error.message}`; + }); await Promise.all(urls.map((url) => queue.push(url))); // Setup the results obj diff --git a/nx/blocks/loc/views/translate/index.js b/nx/blocks/loc/views/translate/index.js index b9ac432a9..54962c63a 100644 --- a/nx/blocks/loc/views/translate/index.js +++ b/nx/blocks/loc/views/translate/index.js @@ -89,9 +89,11 @@ export async function getUrls( } }; - const queue = new Queue(fetchUrl, MAX_CONCURRENT_READS); + const queue = new Queue(fetchUrl, MAX_CONCURRENT_READS, (url, error) => { + url.error = `Error fetching content from ${url.daDestPath} - ${error.message}`; + }); - await Promise.allSettled(formattedUrls.map((url) => queue.push(url))); + await Promise.all(formattedUrls.map((url) => queue.push(url))); } return { urls: formattedUrls }; diff --git a/nx/blocks/loc/views/validate/validate.js b/nx/blocks/loc/views/validate/validate.js index fc6dd4f61..94fc91de5 100644 --- a/nx/blocks/loc/views/validate/validate.js +++ b/nx/blocks/loc/views/validate/validate.js @@ -130,7 +130,11 @@ class NxLocValidate extends LitElement { const checkUrl = this.checkUrl.bind(this); - const queue = new Queue(checkUrl, MAX_CONCURRENT_READS); + const queue = new Queue(checkUrl, MAX_CONCURRENT_READS, (url, error) => { + url.status = `error - ${error.message}`; + url.checked = false; + this.requestUpdate(); + }); let notChecked; while (!notChecked || notChecked.length > 0) { From 7c19a8167b3ef94c34384089d4cb3856aba31693 Mon Sep 17 00:00:00 2001 From: Ben Peter Date: Sat, 28 Nov 2026 12:07:32 +0100 Subject: [PATCH 3/3] fix(loc): show failed project loads and leave vendor reads to their fix --- nx/blocks/loc/loc.js | 12 +++++++++--- nx/blocks/loc/views/translate/index.js | 6 ++---- test/loc/project-load-error.test.js | 22 ++++++++++++++++++++++ test/loc/thrown-reads.test.js | 15 +-------------- 4 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 test/loc/project-load-error.test.js diff --git a/nx/blocks/loc/loc.js b/nx/blocks/loc/loc.js index ab1bd6cad..0bda1f993 100644 --- a/nx/blocks/loc/loc.js +++ b/nx/blocks/loc/loc.js @@ -57,6 +57,7 @@ class NxLoc extends LitElement { path: { attribute: false }, _project: { state: true }, _message: { state: true }, + _loadError: { state: true }, }; connectedCallback() { @@ -71,6 +72,7 @@ class NxLoc extends LitElement { } async getProject() { + this._loadError = undefined; // If there's no path, create a synthetic project if (!this.path) { this._project = { @@ -81,10 +83,12 @@ class NxLoc extends LitElement { return; } - const path = `/${this.org}/${this.site}${this.path}`; + const { org, site, path: projectPath } = this; + const path = `/${org}/${site}${projectPath}`; const { message, project } = await loadProject({ path }); - if (message) this._message = message; + if (this.org !== org || this.site !== site || this.path !== projectPath) return; + if (message) this._loadError = message.text; // The hash-based view should override the project view if (project) this._project = { ...project, view: this.view }; @@ -213,7 +217,9 @@ class NxLoc extends LitElement { } render() { - if (!this._project) return nothing; + if (!this._project) { + return this._loadError ? html`` : nothing; + } return html` diff --git a/nx/blocks/loc/views/translate/index.js b/nx/blocks/loc/views/translate/index.js index 54962c63a..b9ac432a9 100644 --- a/nx/blocks/loc/views/translate/index.js +++ b/nx/blocks/loc/views/translate/index.js @@ -89,11 +89,9 @@ export async function getUrls( } }; - const queue = new Queue(fetchUrl, MAX_CONCURRENT_READS, (url, error) => { - url.error = `Error fetching content from ${url.daDestPath} - ${error.message}`; - }); + const queue = new Queue(fetchUrl, MAX_CONCURRENT_READS); - await Promise.all(formattedUrls.map((url) => queue.push(url))); + await Promise.allSettled(formattedUrls.map((url) => queue.push(url))); } return { urls: formattedUrls }; diff --git a/test/loc/project-load-error.test.js b/test/loc/project-load-error.test.js new file mode 100644 index 000000000..b18e92d46 --- /dev/null +++ b/test/loc/project-load-error.test.js @@ -0,0 +1,22 @@ +import { expect } from '@esm-bundle/chai'; +import '../../nx/blocks/loc/loc.js'; + +describe('loc project load', () => { + it('shows an error when opening a project fails', async () => { + const originalFetch = window.fetch; + const el = document.createElement('nx-loc'); + el.org = 'org'; + el.site = 'site'; + el.path = '/.da/translation/active/1'; + window.fetch = async () => { throw new TypeError('Failed to fetch'); }; + try { + document.body.append(el); + await el.getProject(); + await el.updateComplete; + expect(el.shadowRoot.querySelector('[role="alert"]')?.textContent).to.equal('Could not open the translation project. Failed to fetch'); + } finally { + el.remove(); + window.fetch = originalFetch; + } + }); +}); diff --git a/test/loc/thrown-reads.test.js b/test/loc/thrown-reads.test.js index 22490d434..309533d82 100644 --- a/test/loc/thrown-reads.test.js +++ b/test/loc/thrown-reads.test.js @@ -1,7 +1,6 @@ import { expect } from '@esm-bundle/chai'; import { rolloutLang } from '../../nx/blocks/loc/views/rollout/index.js'; -import { getUrls } from '../../nx/blocks/loc/views/translate/index.js'; -import { clearConfigCache, loadProject } from '../../nx/blocks/loc/utils/utils.js'; +import { loadProject } from '../../nx/blocks/loc/utils/utils.js'; import '../../nx/blocks/loc/views/validate/validate.js'; const message = 'Failed to fetch'; @@ -16,9 +15,6 @@ describe('loc reads that throw', () => { const href = url.toString(); const method = opts.method || 'GET'; calls.push({ href, method }); - if (href.endsWith('/.da/translate.json')) { - return new Response(JSON.stringify({ config: { data: [] } }), { status: 200 }); - } if (method === 'GET') throw new TypeError(message); return new Response('', { status: 200 }); }; @@ -26,19 +22,10 @@ describe('loc reads that throw', () => { beforeEach(() => { origFetch = window.fetch; - clearConfigCache(); }); afterEach(() => { window.fetch = origFetch; - clearConfigCache(); - }); - - it('marks the page as failed in translate', async () => { - failPageReads(); - const { urls } = await getUrls('org', 'site', {}, '/en', '/en', [{ suppliedPath: '/en/page' }], true); - expect(urls[0].content).to.be.undefined; - expect(urls[0].error).to.equal(`Error fetching content from /en/page.html - ${message}`); }); it('stops a rollout before writing anything', async () => {