Skip to content

Commit

Permalink
ELEMENTS-1281: handle errors on bulk action
Browse files Browse the repository at this point in the history
  • Loading branch information
RSalem07 committed Feb 9, 2021
1 parent 7ea0206 commit d9a0050
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
5 changes: 5 additions & 0 deletions core/nuxeo-audit-page-provider.js
Expand Up @@ -63,6 +63,7 @@ import './nuxeo-resource.js';
schemas="[[schemas]]"
loading="{{loading}}"
headers="{{headers}}"
on-error="_preventPropagation"
>
</nuxeo-operation>
`;
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 26 additions & 18 deletions core/nuxeo-operation.js
Expand Up @@ -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"></nuxeo-operation>
*
* With `auto` set to `true`, the operation is executed whenever
Expand Down Expand Up @@ -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;
});
}

Expand All @@ -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();
});
Expand Down
5 changes: 5 additions & 0 deletions core/nuxeo-page-provider.js
Expand Up @@ -79,6 +79,7 @@ import './nuxeo-resource.js';
enrichers="{{enrichers}}"
schemas="[[schemas]]"
headers="{{headers}}"
on-error="_preventPropagation"
>
</nuxeo-operation>
`;
Expand Down Expand Up @@ -509,6 +510,10 @@ import './nuxeo-resource.js';
delete this.headers['fetch-aggregate'];
}
}

_preventPropagation(e) {
e.stopPropagation();
}
}

customElements.define(PageProvider.is, PageProvider);
Expand Down
16 changes: 8 additions & 8 deletions core/test/nuxeo-operation.test.js
Expand Up @@ -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');
});
});
});

Expand Down

0 comments on commit d9a0050

Please sign in to comment.