Home page Forum Wiki Mail Jabber Devel NIX-FILES

AltLUG Git

Git-repositories of Altai Linux User Group

Improve window.onerror handling
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 12 Jun 2012 17:45:34 +0000 (19:45 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 12 Jun 2012 18:02:28 +0000 (20:02 +0200)
* Chain in on other window.onerror handlers and honor their
  return value. Just like we expect other code to keep our onerror
  handler (e.g TestSwarm).

* Added explicit 'return false' for clarity instead of leaving it
  undefined and assuming other handlers understand that.

* Best reviewed with -w :)

* grunt: Passes, without errors.

qunit/qunit.js

index 513c79c..d8a0de0 100644 (file)
@@ -12,6 +12,7 @@
 
 var QUnit,
        config,
+       onErrorFnPrev,
        testId = 0,
        fileName = (sourceFromStacktrace( 0 ) || "" ).replace(/(:\d+)+\)?/, "").replace(/.+\//, ""),
        toString = Object.prototype.toString,
@@ -947,18 +948,36 @@ QUnit.load = function() {
 
 addEvent( window, "load", QUnit.load );
 
-// addEvent(window, "error" ) gives us a useless event object
-window.onerror = function( message, file, line ) {
-       if ( QUnit.config.current ) {
-               if ( QUnit.config.current.ignoreGlobalErrors ) {
-                       return true;
+// `onErrorFnPrev` initialized at top of scope
+// Preserve other handlers
+onErrorFnPrev = window.onerror;
+
+// Cover uncaught exceptions
+// Returning true will surpress the default browser handler,
+// returning false will let it run.
+window.onerror = function ( error, filePath, linerNr ) {
+       var ret = false;
+       if ( onErrorFnPrev ) {
+               ret = onErrorFnPrev( error, filePath, linerNr );
+       }
+
+       // Treat return value as window.onerror itself does,
+       // Only do our handling if not surpressed.
+       if ( ret !== true ) {
+               if ( QUnit.config.current ) {
+                       if ( QUnit.config.current.ignoreGlobalErrors ) {
+                               return true;
+                       }
+                       QUnit.pushFailure( error, filePath + ":" + linerNr );
+               } else {
+                       QUnit.test( "global failure", function() {
+                               QUnit.pushFailure( error, filePath + ":" + linerNr );
+                       });
                }
-               QUnit.pushFailure( message, file + ":" + line );
-       } else {
-               QUnit.test( "global failure", function() {
-                       QUnit.pushFailure( message, file + ":" + line );
-               });
+               return false;
        }
+
+       return ret;
 };
 
 function done() {

Hosting is provided AltSPAHow to use Git
Please direct any questions on jabber alex.wolf@jabber.ru

Valid HTML 4.0 Transitional Valid CSS!

Designer Alexander Wolf