Skip to content

Commit bfbc395

Browse files
committed
Core: Re-throw errors that happened in callbacks wrapped in jQuery ready
Also, expose jQuery.readyException that allows to overwrite the default ready error handler. Fixes gh-3174 Closes gh-3210
1 parent 25d8ccd commit bfbc395

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/core/ready.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
define( [
22
"../core",
33
"../var/document",
4+
"../core/readyException",
45
"../deferred"
56
], function( jQuery, document ) {
67

@@ -11,7 +12,11 @@ var readyList = jQuery.Deferred();
1112

1213
jQuery.fn.ready = function( fn ) {
1314

14-
readyList.then( fn );
15+
readyList
16+
.then( fn )
17+
.catch( function( error ) {
18+
jQuery.readyException( error );
19+
} );
1520

1621
return this;
1722
};

src/core/readyException.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
define( [
2+
"../core"
3+
], function( jQuery ) {
4+
5+
"use strict";
6+
7+
jQuery.readyException = function( error ) {
8+
window.setTimeout( function() {
9+
throw error;
10+
} );
11+
};
12+
13+
} );

test/unit/core.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
QUnit.module( "core", { teardown: moduleTeardown } );
1+
QUnit.module( "core", {
2+
setup: function() {
3+
this.sandbox = sinon.sandbox.create();
4+
},
5+
teardown: function() {
6+
this.sandbox.restore();
7+
return moduleTeardown.apply( this, arguments );
8+
}
9+
} );
210

311
QUnit.test( "Basic requirements", function( assert ) {
412
assert.expect( 7 );
@@ -1709,3 +1717,45 @@ QUnit.test( "Iterability of jQuery objects (gh-1693)", function( assert ) {
17091717
assert.ok( true, "The browser doesn't support Symbols" );
17101718
}
17111719
} );
1720+
1721+
QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (original)", function( assert ) {
1722+
assert.expect( 1 );
1723+
1724+
var message;
1725+
1726+
this.sandbox.stub( window, "setTimeout", function( fn ) {
1727+
try {
1728+
fn();
1729+
} catch ( error ) {
1730+
message = error.message;
1731+
}
1732+
} );
1733+
1734+
jQuery( function() {
1735+
throw new Error( "Error in jQuery ready" );
1736+
} );
1737+
assert.strictEqual(
1738+
message,
1739+
"Error in jQuery ready",
1740+
"The error should have been thrown in a timeout"
1741+
);
1742+
} );
1743+
1744+
QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (custom)", function( assert ) {
1745+
assert.expect( 1 );
1746+
1747+
var done = assert.async();
1748+
1749+
this.sandbox.stub( jQuery, "readyException", function( error ) {
1750+
assert.strictEqual(
1751+
error.message,
1752+
"Error in jQuery ready",
1753+
"The custom jQuery.readyException should have been called"
1754+
);
1755+
done();
1756+
} );
1757+
1758+
jQuery( function() {
1759+
throw new Error( "Error in jQuery ready" );
1760+
} );
1761+
} );

0 commit comments

Comments
 (0)