Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions nx/blocks/loc/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class NxLoc extends LitElement {
path: { attribute: false },
_project: { state: true },
_message: { state: true },
_loadError: { state: true },
};

connectedCallback() {
Expand All @@ -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 = {
Expand All @@ -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 };
Expand Down Expand Up @@ -213,7 +217,9 @@ class NxLoc extends LitElement {
}

render() {
if (!this._project) return nothing;
if (!this._project) {
return this._loadError ? html`<p class="nx-loc-step" role="alert">Could not open the translation project. ${this._loadError}</p>` : nothing;
}

return html`
<nx-loc-header .view=${this.view} .title=${this._project?.title}></nx-loc-header>
Expand Down
4 changes: 3 additions & 1 deletion nx/blocks/loc/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
4 changes: 3 additions & 1 deletion nx/blocks/loc/views/rollout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion nx/blocks/loc/views/validate/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions test/loc/project-load-error.test.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
72 changes: 72 additions & 0 deletions test/loc/thrown-reads.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect } from '@esm-bundle/chai';
import { rolloutLang } from '../../nx/blocks/loc/views/rollout/index.js';
import { 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 (method === 'GET') throw new TypeError(message);
return new Response('', { status: 200 });
};
};

beforeEach(() => {
origFetch = window.fetch;
});

afterEach(() => {
window.fetch = origFetch;
});

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 } });
});
});
Loading