summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.errorLogger.js
blob: 9f4f19ddc395db1faa9cac5bc519932a5321e5d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Try to catch errors in modules which don't do their own error handling.
 * @class mw.errorLogger
 * @singleton
 */
( function ( mw ) {
	'use strict';

	mw.errorLogger = {
		/**
		 * Fired via mw.track when an error is not handled by local code and is caught by the
		 * window.onerror handler.
		 *
		 * @event global_error
		 * @param {string} errorMessage Error errorMessage.
		 * @param {string} url URL where error was raised.
		 * @param {number} lineNumber Line number where error was raised.
		 * @param {number} [columnNumber] Line number where error was raised. Not all browsers
		 *   support this.
		 * @param {Error|Mixed} [errorObject] The error object. Typically an instance of Error, but anything
		 *   (even a primitive value) passed to a throw clause will end up here.
		 */

		/**
		 * Install a window.onerror handler that will report via mw.track, while preserving
		 * any previous handler.
		 * @param {Object} window
		 */
		installGlobalHandler: function ( window ) {
			// We will preserve the return value of the previous handler. window.onerror works the
			// opposite way than normal event handlers (returning true will prevent the default
			// action, returning false will let the browser handle the error normally, by e.g.
			// logging to the console), so our fallback old handler needs to return false.
			var oldHandler = window.onerror || function () { return false; };

			/**
			 * Dumb window.onerror handler which forwards the errors via mw.track.
			 * @fires global_error
			 */
			window.onerror = function ( errorMessage, url, lineNumber, columnNumber, errorObject ) {
				mw.track( 'global.error', { errorMessage: errorMessage, url: url,
					lineNumber: lineNumber, columnNumber: columnNumber, errorObject: errorObject } );
				return oldHandler.apply( this, arguments );
			};
		}
	};

	mw.errorLogger.installGlobalHandler( window );
}( mediaWiki ) );