From d9a00503f70f45e7e3a11cffc98a29d84a62ae05 Mon Sep 17 00:00:00 2001 From: Salem Aouana Date: Mon, 4 Jan 2021 16:08:49 +0100 Subject: [PATCH] ELEMENTS-1281: handle errors on bulk action --- core/nuxeo-audit-page-provider.js | 5 ++++ core/nuxeo-operation.js | 44 ++++++++++++++++++------------- core/nuxeo-page-provider.js | 5 ++++ core/test/nuxeo-operation.test.js | 16 +++++------ 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/core/nuxeo-audit-page-provider.js b/core/nuxeo-audit-page-provider.js index 74f84448bc..5a8ab97c57 100644 --- a/core/nuxeo-audit-page-provider.js +++ b/core/nuxeo-audit-page-provider.js @@ -63,6 +63,7 @@ import './nuxeo-resource.js'; schemas="[[schemas]]" loading="{{loading}}" headers="{{headers}}" + on-error="_preventPropagation" > `; @@ -344,6 +345,10 @@ import './nuxeo-resource.js'; this._debouncer = Debouncer.debounce(this._debouncer, timeOut.after(this.autoDelay), () => this.fetch()); } } + + _preventPropagation(e) { + e.stopPropagation(); + } } customElements.define(AuditPageProvider.is, AuditPageProvider); diff --git a/core/nuxeo-operation.js b/core/nuxeo-operation.js index 5c474abdf9..a9c1a8f26c 100644 --- a/core/nuxeo-operation.js +++ b/core/nuxeo-operation.js @@ -26,6 +26,7 @@ import './nuxeo-connection.js'; * op="Document.Query" * params='{"query": "select * from Document"}' * on-response="handleResponse" + * on-error="handleError" * enrichers="documentURL, preview"> * * With `auto` set to `true`, the operation is executed whenever @@ -308,20 +309,22 @@ import './nuxeo-connection.js'; return this.response; }) .catch((error) => { - if (error.response.status === 401) { - this.dispatchEvent( - new CustomEvent('unauthorized-request', { - bubbles: true, - composed: true, - detail: error, - }), - ); - } + const event = error.response.status === 401 ? 'unauthorized-request' : 'error'; + + this.dispatchEvent( + new CustomEvent(event, { + bubbles: true, + composed: true, + detail: { + error, + }, + }), + ); this.success = false; this.error = error; console.warn(`Operation request failed: ${error}`); this._setActiveRequests(this.activeRequests - 1); - throw error; + throw this.error; }); } @@ -334,15 +337,20 @@ import './nuxeo-connection.js'; } _poll(url) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const fn = () => { - this.$.nx.http(url).then((res) => { - if (this._isRunning(res)) { - window.setTimeout(() => fn(), this.pollInterval, url); - } else { - resolve(res); - } - }); + this.$.nx + .http(url) + .then((res) => { + if (this._isRunning(res)) { + window.setTimeout(() => fn(), this.pollInterval, url); + } else { + resolve(res); + } + }) + .catch((error) => { + reject(error); + }); }; fn(); }); diff --git a/core/nuxeo-page-provider.js b/core/nuxeo-page-provider.js index d727e208b0..4f026c549f 100644 --- a/core/nuxeo-page-provider.js +++ b/core/nuxeo-page-provider.js @@ -79,6 +79,7 @@ import './nuxeo-resource.js'; enrichers="{{enrichers}}" schemas="[[schemas]]" headers="{{headers}}" + on-error="_preventPropagation" > `; @@ -509,6 +510,10 @@ import './nuxeo-resource.js'; delete this.headers['fetch-aggregate']; } } + + _preventPropagation(e) { + e.stopPropagation(); + } } customElements.define(PageProvider.is, PageProvider); diff --git a/core/test/nuxeo-operation.test.js b/core/test/nuxeo-operation.test.js index e687b17b47..56733ed5f0 100644 --- a/core/test/nuxeo-operation.test.js +++ b/core/test/nuxeo-operation.test.js @@ -105,14 +105,14 @@ suite('nuxeo-operation', () => { `, ); - try { - await operation.execute(); - } catch (error) { - expect(error.message).to.be.eq('Internal Server Error'); - return; - } - - throw new Error('Expected to an invalid response!'); + operation + .execute() + .then(() => { + throw new Error('Expected to an invalid response!'); + }) + .catch((error) => { + expect(error.message).to.be.eq('Internal Server Error'); + }); }); });