From a4ac1abec0b875fd480255fd38a516ac490b8598 Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Sat, 25 Aug 2018 22:42:13 +0200 Subject: [PATCH] PolyGerrit: Expose error function in plugin rest API If user optionally pass error handling function to the send method of rest API, request error can be processed in custom elements, e.g.: const errFn = response => { this.fire('page-error', {response}); }; return this.plugin.restApi().send(method, endpoint, body, errFn) .then(r => { Gerrit.Nav.navigateToRelativeUrl('/admin/repos'); }); Considered alternative is to handle errors in plugin requests by providing generic function that does the same: fire page-error event, but having a way to provide custom error function is more flexible approach. Change-Id: Idd7e86dd0c40d53f4327c013fa0bb469ee8a6fff --- .../gr-js-api-interface/gr-plugin-rest-api.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-rest-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-rest-api.js index 57cbc85b4b..c18f7537ed 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-rest-api.js +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-rest-api.js @@ -43,10 +43,14 @@ * @param {string} method HTTP Method (GET, POST, etc) * @param {string} url URL without base path or plugin prefix * @param {Object=} payload Respected for POST and PUT only. + * @param {?function(?Response, string=)=} opt_errFn + * passed as null sometimes. * @return {!Promise} */ - GrPluginRestApi.prototype.fetch = function(method, url, opt_payload) { - return getRestApi().send(method, this.opt_prefix + url, opt_payload); + GrPluginRestApi.prototype.fetch = function(method, url, opt_payload, + opt_errFn) { + return getRestApi().send(method, this.opt_prefix + url, opt_payload, + opt_errFn); }; /** @@ -54,10 +58,13 @@ * @param {string} method HTTP Method (GET, POST, etc) * @param {string} url URL without base path or plugin prefix * @param {Object=} payload Respected for POST and PUT only. + * @param {?function(?Response, string=)=} opt_errFn + * passed as null sometimes. * @return {!Promise} resolves on success, rejects on error. */ - GrPluginRestApi.prototype.send = function(method, url, opt_payload) { - return this.fetch(method, url, opt_payload).then(response => { + GrPluginRestApi.prototype.send = function(method, url, opt_payload, + opt_errFn) { + return this.fetch(method, url, opt_payload, opt_errFn).then(response => { if (response.status < 200 || response.status >= 300) { return response.text().then(text => { if (text) {