Allow subscribing to module loading exceptions
authorGergő Tisza <tgr.huwiki@gmail.com>
Wed, 14 Jan 2015 07:09:41 +0000 (07:09 +0000)
committerOri.livneh <ori@wikimedia.org>
Wed, 25 Feb 2015 02:46:46 +0000 (02:46 +0000)
Provide access via mw.track to module loading errors which are
otherwise swallowed by mw.loader.

Bug: T513
Bug: T85263
Change-Id: Ifc13c207af36dc56be931a3b140063354513a4ec

resources/src/mediawiki/mediawiki.js

index 90b8503..2e78c1c 100644 (file)
                 */
                loader: ( function () {
 
+                       /**
+                        * Fired via mw.track on various resource loading errors.
+                        *
+                        * @event resourceloader_exception
+                        * @param {Error|Mixed} e The error that was thrown. Almost always an Error
+                        *   object, but in theory module code could manually throw something else, and that
+                        *   might also end up here.
+                        * @param {string} [module] Name of the module which caused the error. Omitted if the
+                        *   error is not module-related or the module cannot be easily identified due to
+                        *   batched handling.
+                        * @param {string} source Source of the error. Possible values:
+                        *   - style: stylesheet error (only affects old IE where a special style loading method
+                        *     is used)
+                        *   - load-callback: exception thrown by user callback
+                        *   - module-execute: exception thrown by module code
+                        */
+
                        /**
                         * Mapping of registered modules.
                         *
                                                                styleEl.styleSheet.cssText += cssText;
                                                        } catch ( e ) {
                                                                log( 'Stylesheet error', e );
+                                                               mw.track( 'resourceloader.exception', { exception: e, source: 'stylesheet' } );
                                                        }
                                                } else {
                                                        styleEl.appendChild( document.createTextNode( cssText ) );
                                                        // A user-defined callback raised an exception.
                                                        // Swallow it to protect our state machine!
                                                        log( 'Exception thrown by user callback', e );
+                                                       mw.track( 'resourceloader.exception',
+                                                               { exception: e, module: module, source: 'load-callback' } );
                                                }
                                        }
                                }
                                                // and not in debug mode, such as when a symbol that should be global isn't exported
                                                log( 'Exception thrown by ' + module, e );
                                                registry[module].state = 'error';
+                                               mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
                                                handlePending( module );
                                        }
                                }