diff options
author | Matthieu Aubry <mattab@users.noreply.github.com> | 2016-07-13 06:59:01 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-13 06:59:01 +0300 |
commit | 62116f3535adc1da53d0da922c24fe37589017a8 (patch) | |
tree | f073cf5defcb24171036b862d1c96412fc3558c0 /js | |
parent | dad0dae15cd8b62dbc91125e2d0e4078909aab73 (diff) |
Calling an undefined JS API method should not trigger an error in the console (#10176)
* When calling a non existing method, do not fail JS execution but print a useful error message in browser console
* Fix
* Throw an error so developers can see in the browser console
Diffstat (limited to 'js')
-rw-r--r-- | js/piwik.js | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/js/piwik.js b/js/piwik.js index b92c29b235..df079ddb18 100644 --- a/js/piwik.js +++ b/js/piwik.js @@ -1156,6 +1156,17 @@ if (typeof window.Piwik !== 'object') { return isEmpty; } + /** + * Logs an error in the console. + * Note: it does not generate a JavaScript error, so make sure to also generate an error if needed. + * @param message + */ + function logConsoleError(message) { + if (console !== undefined && console && console.error) { + console.error(message); + } + } + /* * apply wrapper * @@ -1173,11 +1184,20 @@ if (typeof window.Piwik !== 'object') { for (j = 0; j < asyncTrackers.length; j++) { if (isString(f)) { - asyncTrackers[j][f].apply(asyncTrackers[j], parameterArray); + + if(asyncTrackers[j][f]) { + asyncTrackers[j][f].apply(asyncTrackers[j], parameterArray); + } else { + var message = 'The method \'' + f + '\' was not found in "_paq" variable. Please have a look at the Piwik tracker documentation: http://developer.piwik.org/api-reference/tracking-javascript'; + logConsoleError(message); + throw new TypeError(message); + } + if (f === 'addTracker') { // addTracker adds an entry to asyncTrackers and would otherwise result in an endless loop break; } + if (f === 'setTrackerUrl' || f === 'setSiteId') { // these two methods should be only executed on the first tracker break; @@ -6414,9 +6434,7 @@ if (typeof window.Piwik !== 'object') { delete paq[iterator]; if (appliedMethods[methodName] > 1) { - if (console !== undefined && console && console.error) { - console.error('The method ' + methodName + ' is registered more than once in "paq" variable. Only the last call has an effect. Please have a look at the multiple Piwik trackers documentation: http://developer.piwik.org/guides/tracking-javascript-guide#multiple-piwik-trackers'); - } + logConsoleError('The method ' + methodName + ' is registered more than once in "_paq" variable. Only the last call has an effect. Please have a look at the multiple Piwik trackers documentation: http://developer.piwik.org/guides/tracking-javascript-guide#multiple-piwik-trackers'); } appliedMethods[methodName]++; |