jsduck: Refactor example iframe to catch uncaught errors
authorTimo Tijhof <krinklemail@gmail.com>
Sun, 8 Mar 2015 17:38:01 +0000 (18:38 +0100)
committerKrinkle <krinklemail@gmail.com>
Wed, 11 Mar 2015 14:22:43 +0000 (14:22 +0000)
* Make logging function not depend on 'mw' existing.
* Add window.onerror handler.
* Keep exporting to mw.log because that's what existing examples
  use and is what uses should copy for interoperability with the
  regular runtime.

Change-Id: I2925532035974343125e34084f9ac7c8dd1fa279

maintenance/jsduck/eg-iframe.html

index 0792c24..50d7921 100644 (file)
@@ -48,9 +48,9 @@
        /**
         * Basic log console for the example iframe in documentation pages.
         */
-       ( function () {
+       var log = ( function () {
                var pre;
-               mw.log = function () {
+               return function () {
                        var str, i, len, line;
                        if ( !pre ) {
                                pre = document.createElement( 'pre' );
                };
        }() );
 
+       if ( window.mw ) {
+               mw.log = log;
+       }
+
+       window.onerror = function ( error, filePath, linerNr ) {
+               log( filePath + ':' + linerNr  );
+       };
+
        /**
         * Method called by jsduck to execute the example code.
         */
@@ -78,7 +86,7 @@
                        eval( code );
                        callback && callback( true );
                } catch ( e ) {
-                       mw.log( 'Uncaught ' + e );
+                       log( 'Uncaught ' + e );
                        callback && callback( false, e );
                        throw e;
                }