Skip to content

Commit 23f305c

Browse files
committed
Define getErrorHook, fix tests, update README
1 parent 8f924ae commit 23f305c

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# jquery-deferred-reporter
22

3-
This jQuery plugin extends the `jQuery.Deferred` features added in version 3.0 to include a stack trace whenever a Deferred throws an exception. This makes it easier to find programming errors that occur inside Deferreds.
3+
This jQuery plugin extends the `jQuery.Deferred` features added in version 3.0 to include an error captured before the async barrier whenever a Deferred throws an exception. This makes it easier to find programming errors that occur inside Deferreds.
44

55
## Why does this plugin exist?
66

@@ -10,6 +10,4 @@ The native `Promise` object as implemented in the browser tracks Promise rejecti
1010

1111
## Why not just put this in jQuery?
1212

13-
Since it has to save the stack trace regardless of whether an exception will happen or not, adding this plugin makes `jQuery.Deferred` [significantly slower](https://jsfiddle.net/h20r0e6z/5/), by roughly a factor of two.
14-
15-
13+
Since it has to save the error regardless of whether an exception will happen or not, adding this plugin makes `jQuery.Deferred` [significantly slower](https://jsfiddle.net/h20r0e6z/5/), by roughly a factor of two.

src/reporter.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
( function( factory ) {
2+
"use strict";
23

3-
if ( typeof define === "function" && define.amd ) {
4-
define( "jquery-deferred-reporter", [ "jquery" ], factory );
5-
} else if ( typeof module === "object" && module.exports ) {
6-
module.exports = factory( require( "jquery" ) );
7-
} else {
8-
factory( jQuery );
9-
}
104

11-
}( function( jQuery ) {
5+
if ( typeof define === "function" && define.amd ) {
6+
define( "jquery-deferred-reporter", [ "jquery" ], factory );
7+
} else if ( typeof module === "object" && module.exports ) {
8+
module.exports = factory( require( "jquery" ) );
9+
} else {
10+
factory( jQuery );
11+
}
12+
13+
} )( function( jQuery ) {
14+
"use strict";
1215

13-
function getStackHook() {
16+
function getErrorHook() {
1417

15-
// Throw an error so we can extract the stack from the Error
18+
// Throw an error as IE doesn't capture `stack` of non-thrown ones.
1619
try {
1720
throw new Error( "Exception in jQuery.Deferred" );
1821
} catch ( err ) {
19-
return err.stack;
22+
return err;
2023
}
2124
}
22-
return jQuery.Deferred.getStackHook = getStackHook;
2325

24-
} ) );
26+
// Define both `jQuery.Deferred.getErrorHook` used in jQuery >=3.7.0
27+
// and `jQuery.Deferred.getStackHook` used in jQuery <4.0.0.
28+
return jQuery.Deferred.getStackHook = jQuery.Deferred.getErrorHook = getErrorHook;
29+
30+
} );

test/test.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,82 @@
1+
"use strict";
12

2-
QUnit.test( "Sanity", function( assert ) {
3+
// Polyfill `globalThis` for legacy browsers.
4+
if ( typeof globalThis === "undefined" ) {
5+
window.globalThis = window;
6+
}
7+
8+
QUnit.test( "Environment check", function( assert ) {
39
assert.expect( 1 );
4-
assert.ok( Array.prototype.push, "Array.push()" );
10+
assert.ok( Array.prototype.push, "Array#push()" );
511
} );
612

7-
QUnit[ window.console ? "test" : "skip" ](
13+
QUnit[ globalThis.console ? "test" : "skip" ](
814
"jQuery.Deferred.exceptionHook",
915
function exceptionHookTest( assert ) {
1016

1117
assert.expect( 1 );
1218

1319
var done = assert.async(),
1420
defer = jQuery.Deferred(),
15-
oldWarn = window.console.warn;
21+
oldWarn = globalThis.console.warn;
22+
23+
if ( jQuery.fn.jquery.indexOf( "3." ) === 0 ) {
24+
globalThis.console.warn = function( msg ) {
25+
assert.ok( /barf/.test( msg ), "Message: " + msg );
26+
};
27+
} else {
28+
globalThis.console.warn = function( _intro, error ) {
29+
assert.ok( /barf/.test( error.message + "\n" + error.stack ),
30+
"Error mentions the method: " + error.message + "\n" + error.stack );
31+
};
32+
}
1633

17-
window.console.warn = function( msg ) {
18-
assert.ok( /barf/.test( msg ), "Message: " + msg );
19-
};
2034
jQuery.when(
2135
defer.then( function() {
36+
2237
// Should get an error
2338
jQuery.barf();
2439
} ).then( null, jQuery.noop ),
2540
defer.then( function() {
41+
2642
// Should NOT get an error
2743
throw new Error( "Make me a sandwich" );
2844
} ).then( null, jQuery.noop )
2945
).then( function( ) {
30-
window.console.warn = oldWarn;
46+
globalThis.console.warn = oldWarn;
3147
done();
3248
} );
3349

3450
defer.resolve();
3551
} );
3652

37-
QUnit[ window.console ? "test" : "skip" ](
53+
QUnit[ globalThis.console ? "test" : "skip" ](
3854
"jQuery.Deferred.exceptionHook with stack hooks",
3955
function exceptionHookWithStack( assert ) {
4056

4157
assert.expect( 2 );
4258

4359
var done = assert.async(),
4460
defer = jQuery.Deferred(),
45-
oldWarn = window.console.warn;
61+
oldWarn = globalThis.console.warn;
62+
63+
globalThis.console.warn = function( intro, error, asyncError ) {
64+
assert.ok(
65+
/cough_up_hairball/.test( intro + "\n" + ( error && error.message || error ) ),
66+
"Function mentioned: " + intro + "\n" + ( error && error.message || error )
67+
);
4668

47-
window.console.warn = function( msg, stack ) {
48-
assert.ok( /cough_up_hairball/.test( msg ), "Function mentioned: " + msg );
49-
assert.ok( /exceptionHookWithStack/.test( stack ), "Stack trace included: \n" + stack );
69+
assert.ok(
70+
/exceptionHookWithStack/.test( asyncError.message + "\n" + asyncError.stack ),
71+
"Stack trace included: " + asyncError.message + "\n" + asyncError.stack
72+
);
5073
};
74+
5175
defer.then( function() {
5276
jQuery.cough_up_hairball();
5377
} ).then( null, function( ) {
54-
window.console.warn = oldWarn;
55-
delete jQuery.Deferred.getStackHook;
78+
globalThis.console.warn = oldWarn;
79+
delete jQuery.Deferred.getErrorHook;
5680
done();
5781
} );
5882

0 commit comments

Comments
 (0)