From 5d8f340a1f114f76d246660710cee113c735540f Mon Sep 17 00:00:00 2001 From: dizzy Date: Mon, 11 Apr 2022 09:58:22 -0700 Subject: =?UTF-8?q?[Vue]=20add=20piwik-api.js=20error=20handling=20logic?= =?UTF-8?q?=20to=20AjaxHelper.fetch=20if=20createErrorNotification=20is=20?= =?UTF-8?q?set=20to=20false=E2=80=A6=20(#19071)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add piwik-api.js error handling logic to AjaxHelper.fetch if createErrorNotification is set to false and do not call AjaxHelper errorCallback in this case too * undo submodule change --- plugins/CoreHome/vue/dist/CoreHome.umd.js | 24 +++++++++++++++++--- plugins/CoreHome/vue/dist/CoreHome.umd.min.js | 2 +- plugins/CoreHome/vue/src/AjaxHelper/AjaxHelper.ts | 27 ++++++++++++++++++----- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/plugins/CoreHome/vue/dist/CoreHome.umd.js b/plugins/CoreHome/vue/dist/CoreHome.umd.js index 3370fdce82..3c9e7a3f5a 100644 --- a/plugins/CoreHome/vue/dist/CoreHome.umd.js +++ b/plugins/CoreHome/vue/dist/CoreHome.umd.js @@ -1796,10 +1796,12 @@ var AjaxHelper_AjaxHelper = /*#__PURE__*/function () { resolve(data); // ignoring textStatus/jqXHR } }).fail(function (xhr) { - if (xhr.statusText !== 'abort') { - console.log("Warning: the ".concat($.param(_this2.getParams), " request failed!")); - reject(xhr); + if (xhr.statusText === 'abort') { + return; } + + console.log("Warning: the ".concat($.param(_this2.getParams), " request failed!")); + reject(xhr); }).done(function () { if ($timeout) { $timeout(); // trigger digest @@ -2103,8 +2105,12 @@ var AjaxHelper_AjaxHelper = /*#__PURE__*/function () { helper.headers = options.headers; } + var createErrorNotification = true; + if (typeof options.createErrorNotification !== 'undefined' && !options.createErrorNotification) { helper.useCallbackInCaseOfError(); + helper.setErrorCallback(null); + createErrorNotification = false; } if (options.abortController) { @@ -2123,6 +2129,18 @@ var AjaxHelper_AjaxHelper = /*#__PURE__*/function () { } return result; + }).catch(function (xhr) { + if (createErrorNotification) { + throw xhr; + } + + var message = 'Something went wrong'; + + if (xhr.status === 504) { + message = 'Request was prossibly aborted'; + } + + throw new Error(message); }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/plugins/CoreHome/vue/dist/CoreHome.umd.min.js b/plugins/CoreHome/vue/dist/CoreHome.umd.min.js index e23e1c33db..c614a5f418 100644 --- a/plugins/CoreHome/vue/dist/CoreHome.umd.min.js +++ b/plugins/CoreHome/vue/dist/CoreHome.umd.min.js @@ -99,7 +99,7 @@ function Pe(){return S}function Ve(e,t){t.$oldEmit=t.$emit,t.$emit=function(e){f * * @link https://matomo.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */function Ke(e,t){if("abort"!==t)if("undefined"!==typeof Piwik_Popover){var n=$("#loadingError");Piwik_Popover.isOpen()&&e&&500===e.status?e&&500===e.status&&$(document.body).html(piwikHelper.escape(e.responseText)):n.show()}else console.log("Request failed: ".concat(e.responseText))}Oe.updatePeriodParamsFromUrl=Ce.updatePeriodParamsFromUrl.bind(Ce),window.angular.module("piwikApp.service").service("piwikUrl",De),window.angular.module("piwikApp.service").run(["$location",function(){return null}]),window.angular.module("piwikApp.service").service("piwik",Pe),Ve.$inject=["piwik","$rootScope"],window.angular.module("piwikApp.service").run(Ve),window.globalAjaxQueue=[],window.globalAjaxQueue.active=0,window.globalAjaxQueue.clean=function(){for(var e=this.length;e>=0;e-=1)this[e]&&4!==this[e].readyState||this.splice(e,1)},window.globalAjaxQueue.push=function(){for(var e,t=arguments.length,n=new Array(t),r=0;r1&&void 0!==arguments[1]?arguments[1]:{},r=new e;return n.withTokenInUrl&&r.withTokenInUrl(),n.errorElement&&r.setErrorElement(n.errorElement),n.redirectOnSuccess&&r.redirectOnSuccess(!0!==n.redirectOnSuccess?n.redirectOnSuccess:void 0),r.setFormat(n.format||"json"),Array.isArray(t)?r.setBulkRequests.apply(r,Te(t)):r.addParams(Object.assign(Object.assign({module:"API",format:n.format||"json"},t),{},{segment:t.segment?encodeURIComponent(t.segment):void 0}),"get"),n.postParams&&r.addParams(n.postParams,"post"),n.headers&&(r.headers=n.headers),"undefined"===typeof n.createErrorNotification||n.createErrorNotification||r.useCallbackInCaseOfError(),n.abortController&&(r.abortController=n.abortController),n.returnResponseObject&&(r.resolveWithHelper=!0),r.send().then((function(t){var n=t instanceof e?t.requestHandle.responseJSON:t;if("error"===n.result)throw new Xe(n.message);return t}))}},{key:"post",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.fetch(e,Object.assign(Object.assign({},n),{},{postParams:t}))}}]),e}();function et(){return window.globalAjaxQueue}function tt(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function nt(e,t){for(var n=0;n=0;e-=1)this[e]&&4!==this[e].readyState||this.splice(e,1)},window.globalAjaxQueue.push=function(){for(var e,t=arguments.length,n=new Array(t),r=0;r1&&void 0!==arguments[1]?arguments[1]:{},r=new e;n.withTokenInUrl&&r.withTokenInUrl(),n.errorElement&&r.setErrorElement(n.errorElement),n.redirectOnSuccess&&r.redirectOnSuccess(!0!==n.redirectOnSuccess?n.redirectOnSuccess:void 0),r.setFormat(n.format||"json"),Array.isArray(t)?r.setBulkRequests.apply(r,Te(t)):r.addParams(Object.assign(Object.assign({module:"API",format:n.format||"json"},t),{},{segment:t.segment?encodeURIComponent(t.segment):void 0}),"get"),n.postParams&&r.addParams(n.postParams,"post"),n.headers&&(r.headers=n.headers);var i=!0;return"undefined"===typeof n.createErrorNotification||n.createErrorNotification||(r.useCallbackInCaseOfError(),r.setErrorCallback(null),i=!1),n.abortController&&(r.abortController=n.abortController),n.returnResponseObject&&(r.resolveWithHelper=!0),r.send().then((function(t){var n=t instanceof e?t.requestHandle.responseJSON:t;if("error"===n.result)throw new Xe(n.message);return t})).catch((function(e){if(i)throw e;var t="Something went wrong";throw 504===e.status&&(t="Request was prossibly aborted"),new Error(t)}))}},{key:"post",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.fetch(e,Object.assign(Object.assign({},n),{},{postParams:t}))}}]),e}();function et(){return window.globalAjaxQueue}function tt(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function nt(e,t){for(var n=0;n { // eslint-disable-line * * @deprecated use the jquery promise API */ - errorCallback: AnyFunction; + errorCallback: AnyFunction|null; withToken = false; @@ -220,10 +220,13 @@ export default class AjaxHelper { // eslint-disable-line helper.headers = options.headers; } + let createErrorNotification = true; if (typeof options.createErrorNotification !== 'undefined' && !options.createErrorNotification ) { helper.useCallbackInCaseOfError(); + helper.setErrorCallback(null); + createErrorNotification = false; } if (options.abortController) { @@ -243,6 +246,16 @@ export default class AjaxHelper { // eslint-disable-line } return result as R; + }).catch((xhr: jqXHR) => { + if (createErrorNotification) { + throw xhr; + } + + let message = 'Something went wrong'; + if (xhr.status === 504) { + message = 'Request was prossibly aborted'; + } + throw new Error(message); }); } @@ -364,7 +377,7 @@ export default class AjaxHelper { // eslint-disable-line * * @deprecated use the jquery promise API */ - setErrorCallback(callback: AnyFunction): void { + setErrorCallback(callback: AnyFunction|null): void { this.errorCallback = callback; } @@ -477,11 +490,13 @@ export default class AjaxHelper { // eslint-disable-line resolve(data as (T | ErrorResponse)); // ignoring textStatus/jqXHR } }).fail((xhr: jqXHR) => { - if (xhr.statusText !== 'abort') { - console.log(`Warning: the ${$.param(this.getParams)} request failed!`); - - reject(xhr); + if (xhr.statusText === 'abort') { + return; } + + console.log(`Warning: the ${$.param(this.getParams)} request failed!`); + + reject(xhr); }).done(() => { if ($timeout) { $timeout(); // trigger digest -- cgit v1.2.3