]> source.dussan.org Git - jquery.git/commitdiff
Core: Re-throw errors that happened in callbacks wrapped in jQuery ready 3210/head
authorMichał Gołębiowski <m.goleb@gmail.com>
Wed, 29 Jun 2016 12:19:04 +0000 (14:19 +0200)
committerMichał Gołębiowski <m.goleb@gmail.com>
Thu, 7 Jul 2016 08:23:06 +0000 (10:23 +0200)
Also, expose jQuery.readyException that allows to overwrite the default
ready error handler.

Fixes gh-3174
Closes gh-3210

src/core/ready.js
src/core/readyException.js [new file with mode: 0644]
test/unit/core.js

index bb4e222d35ac6678dec300c6f4588563cc4f024a..53b1b2da7504191253d0436aa35521a857b2b1e2 100644 (file)
@@ -1,6 +1,7 @@
 define( [
        "../core",
        "../var/document",
+       "../core/readyException",
        "../deferred"
 ], function( jQuery, document ) {
 
@@ -11,7 +12,15 @@ var readyList = jQuery.Deferred();
 
 jQuery.fn.ready = function( fn ) {
 
-       readyList.then( fn );
+       readyList
+               .then( fn )
+
+               // Wrap jQuery.readyException in a function so that the lookup
+               // happens at the time of error handling instead of callback
+               // registration.
+               .catch( function( error ) {
+                       jQuery.readyException( error );
+               } );
 
        return this;
 };
diff --git a/src/core/readyException.js b/src/core/readyException.js
new file mode 100644 (file)
index 0000000..72bdd90
--- /dev/null
@@ -0,0 +1,13 @@
+define( [
+       "../core"
+], function( jQuery ) {
+
+"use strict";
+
+jQuery.readyException = function( error ) {
+       window.setTimeout( function() {
+               throw error;
+       } );
+};
+
+} );
index 8ad3c86ce238f70a0ea9c9937a746a93e2e4492f..8c0f8640ee29dca27784e9165f384eea8920ec3e 100644 (file)
@@ -1,4 +1,12 @@
-QUnit.module( "core", { teardown: moduleTeardown } );
+QUnit.module( "core", {
+       setup: function() {
+               this.sandbox = sinon.sandbox.create();
+       },
+       teardown: function() {
+               this.sandbox.restore();
+               return moduleTeardown.apply( this, arguments );
+       }
+} );
 
 QUnit.test( "Basic requirements", function( assert ) {
        assert.expect( 7 );
@@ -1709,3 +1717,45 @@ QUnit.test( "Iterability of jQuery objects (gh-1693)", function( assert ) {
                assert.ok( true, "The browser doesn't support Symbols" );
        }
 } );
+
+QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (original)", function( assert ) {
+       assert.expect( 1 );
+
+       var message;
+
+       this.sandbox.stub( window, "setTimeout", function( fn ) {
+               try {
+                       fn();
+               } catch ( error ) {
+                       message = error.message;
+               }
+       } );
+
+       jQuery( function() {
+               throw new Error( "Error in jQuery ready" );
+       } );
+       assert.strictEqual(
+               message,
+               "Error in jQuery ready",
+               "The error should have been thrown in a timeout"
+       );
+} );
+
+QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (custom)", function( assert ) {
+       assert.expect( 1 );
+
+       var done = assert.async();
+
+       this.sandbox.stub( jQuery, "readyException", function( error ) {
+               assert.strictEqual(
+                       error.message,
+                       "Error in jQuery ready",
+                       "The custom jQuery.readyException should have been called"
+               );
+               done();
+       } );
+
+       jQuery( function() {
+               throw new Error( "Error in jQuery ready" );
+       } );
+} );