| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Fired via mw.track when an error is not handled by local code and is caught by the |
| 5 | * window.onerror handler. |
| 6 | * |
| 7 | * @ignore |
| 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 |
| 13 | * support this. |
| 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. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Fired via mw.track when an error is logged with mw.errorLogger#logError. |
| 20 | * |
| 21 | * @ignore |
| 22 | * @event ~'error.caught' |
| 23 | * @param {Error} errorObject The error object |
| 24 | */ |
| 25 | |
| 26 | /** |
| 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. |
| 30 | * |
| 31 | * @private |
| 32 | * @param {Object} window |
| 33 | */ |
| 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 () { |
| 40 | return false; |
| 41 | }; |
| 42 | |
| 43 | window.onerror = function ( errorMessage, url, line, column, errorObject ) { |
| 44 | mw.track( 'global.error', { |
| 45 | errorMessage: errorMessage, |
| 46 | url: url, |
| 47 | lineNumber: line, |
| 48 | columnNumber: column, |
| 49 | stackTrace: errorObject ? errorObject.stack : '', |
| 50 | errorObject: errorObject |
| 51 | } ); |
| 52 | |
| 53 | if ( errorObject ) { |
| 54 | mw.track( 'error.uncaught', errorObject ); |
| 55 | } |
| 56 | |
| 57 | return oldHandler.apply( this, arguments ); |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Allows the logging of client errors for later inspections. |
| 63 | * |
| 64 | * @namespace mw.errorLogger |
| 65 | */ |
| 66 | mw.errorLogger = { |
| 67 | /** |
| 68 | * Logs an error by notifying subscribers to the given mw.track() topic |
| 69 | * (by default `error.caught`) that an event has occurred. |
| 70 | * |
| 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). |
| 75 | */ |
| 76 | logError: function ( error, topic ) { |
| 77 | mw.track( topic || 'error.caught', error ); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | if ( window.QUnit ) { |
| 82 | mw.errorLogger.installGlobalHandler = installGlobalHandler; |
| 83 | } else { |
| 84 | installGlobalHandler( window ); |
| 85 | } |