Merge "Replace infobox usages and extend successbox, warningbox and errorbox"
[lhc/web/wiklou.git] / resources / src / mediawiki.base / 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 mw.errorLogger = {
8 /**
9 * Fired via mw.track when an error is not handled by local code and is caught by the
10 * window.onerror handler.
11 *
12 * @event global_error
13 * @param {string} errorMessage Error errorMessage.
14 * @param {string} url URL where error was raised.
15 * @param {number} lineNumber Line number where error was raised.
16 * @param {number} [columnNumber] Line number where error was raised. Not all browsers
17 * support this.
18 * @param {Error|Mixed} [errorObject] The error object. Typically an instance of Error, but anything
19 * (even a primitive value) passed to a throw clause will end up here.
20 */
21
22 /**
23 * Install a window.onerror handler that will report via mw.track, while preserving
24 * any previous handler.
25 *
26 * @param {Object} window
27 */
28 installGlobalHandler: function ( window ) {
29 // We will preserve the return value of the previous handler. window.onerror works the
30 // opposite way than normal event handlers (returning true will prevent the default
31 // action, returning false will let the browser handle the error normally, by e.g.
32 // logging to the console), so our fallback old handler needs to return false.
33 var oldHandler = window.onerror || function () {
34 return false;
35 };
36
37 /**
38 * Dumb window.onerror handler which forwards the errors via mw.track.
39 *
40 * @param {string} errorMessage
41 * @param {string} url
42 * @param {number} lineNumber
43 * @param {number} [columnNumber]
44 * @param {Error|Mixed} [errorObject]
45 * @return {boolean} True to prevent the default action
46 * @fires global_error
47 */
48 window.onerror = function ( errorMessage, url, lineNumber, columnNumber, errorObject ) {
49 mw.track( 'global.error', {
50 errorMessage: errorMessage,
51 url: url,
52 lineNumber: lineNumber,
53 columnNumber: columnNumber,
54 errorObject: errorObject
55 } );
56 return oldHandler.apply( this, arguments );
57 };
58 }
59 };
60
61 mw.errorLogger.installGlobalHandler( window );