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
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user