4 * Fired via mw.track when an error is not handled by local code and is caught by the
5 * window.onerror handler.
8 * @event ~'global.error'
9 * @param {string} errorMessage Error message.
10 * @param {string} url URL where error was raised.
11 * @param {number} line Line number where error was raised.
12 * @param {number} [column] Line number where error was raised. Not all browsers
14 * @param {Error|Mixed} [errorObject] The error object. Typically an instance of Error, but
15 * anything (even a primitive value) passed to a throw clause will end up here.
19 * Fired via mw.track when an error is logged with mw.errorLogger#logError.
22 * @event ~'error.caught'
23 * @param {Error} errorObject The error object
27 * Install a `window.onerror` handler that logs errors by notifying both `global.error` and
28 * `error.uncaught` topic subscribers that an event has occurred. Note well that the former is
29 * done for backwards compatibilty.
32 * @param {Object} window
34 function installGlobalHandler( window ) {
35 // We will preserve the return value of the previous handler. window.onerror works the
36 // opposite way than normal event handlers (returning true will prevent the default
37 // action, returning false will let the browser handle the error normally, by e.g.
38 // logging to the console), so our fallback old handler needs to return false.
39 const oldHandler = window.onerror || function () {
43 window.onerror = function ( errorMessage, url, line, column, errorObject ) {
44 mw.track( 'global.error', {
45 errorMessage: errorMessage,
49 stackTrace: errorObject ? errorObject.stack : '',
50 errorObject: errorObject
54 mw.track( 'error.uncaught', errorObject );
57 return oldHandler.apply( this, arguments );
62 * Allows the logging of client errors for later inspections.
64 * @namespace mw.errorLogger
68 * Logs an error by notifying subscribers to the given mw.track() topic
69 * (by default `error.caught`) that an event has occurred.
71 * @param {Error} error
72 * @param {string} [topic='error.caught'] Error topic. Conventionally in the form
73 * 'error.⧼component⧽' (where ⧼component⧽ identifies the code logging the error at a
74 * high level; e.g. an extension name).
76 logError: function ( error, topic ) {
77 mw.track( topic || 'error.caught', error );
82 mw.errorLogger.installGlobalHandler = installGlobalHandler;
84 installGlobalHandler( window );