Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.errorLogger.js
1 /**
2 * Try to catch errors in modules which don't do their own error handling.
3 *
4 * @class mw.errorLogger
5 * @singleton
6 */
7 ( function ( mw ) {
8 'use strict';
9
10 mw.errorLogger = {
11 /**
12 * Fired via mw.track when an error is not handled by local code and is caught by the
13 * window.onerror handler.
14 *
15 * @event global_error
16 * @param {string} errorMessage Error errorMessage.
17 * @param {string} url URL where error was raised.
18 * @param {number} lineNumber Line number where error was raised.
19 * @param {number} [columnNumber] Line number where error was raised. Not all browsers
20 * support this.
21 * @param {Error|Mixed} [errorObject] The error object. Typically an instance of Error, but anything
22 * (even a primitive value) passed to a throw clause will end up here.
23 */
24
25 /**
26 * Install a window.onerror handler that will report via mw.track, while preserving
27 * any previous handler.
28 *
29 * @param {Object} window
30 */
31 installGlobalHandler: function ( window ) {
32 // We will preserve the return value of the previous handler. window.onerror works the
33 // opposite way than normal event handlers (returning true will prevent the default
34 // action, returning false will let the browser handle the error normally, by e.g.
35 // logging to the console), so our fallback old handler needs to return false.
36 var oldHandler = window.onerror || function () { return false; };
37
38 /**
39 * Dumb window.onerror handler which forwards the errors via mw.track.
40 *
41 * @fires global_error
42 */
43 window.onerror = function ( errorMessage, url, lineNumber, columnNumber, errorObject ) {
44 mw.track( 'global.error', { errorMessage: errorMessage, url: url,
45 lineNumber: lineNumber, columnNumber: columnNumber, errorObject: errorObject } );
46 return oldHandler.apply( this, arguments );
47 };
48 }
49 };
50
51 mw.errorLogger.installGlobalHandler( window );
52 }( mediaWiki ) );