Skip to content

Commit 07c11c0

Browse files
committed
Deferred: Give better stack diagnostics on exceptions
Ref jquerygh-2736 The exception stack has the name of the immediately outer function where the exception occurred, which can be very handy for tracing errors. Since we already have the exception object we might as well use it.
1 parent b8bd481 commit 07c11c0

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/deferred/exceptionHook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jQuery.Deferred.exceptionHook = function( error, stack ) {
1414
// Support: IE 8 - 9 only
1515
// Console exists when dev tools are open, which can happen at any time
1616
if ( window.console && window.console.warn && error && rerrorNames.test( error.name ) ) {
17-
window.console.warn( "jQuery.Deferred exception: " + error.message, stack );
17+
window.console.warn( "jQuery.Deferred exception: " + error.message, error.stack, stack );
1818
}
1919
};
2020

test/unit/deferred.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,28 +542,30 @@ QUnit.test( "jQuery.Deferred.then - spec compatibility", function( assert ) {
542542
} catch ( _ ) {}
543543
} );
544544

545+
// Test fails in IE9 but is skipped there because console is not active
545546
QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook", function( assert ) {
546547

547-
assert.expect( 1 );
548+
assert.expect( 2 );
548549

549550
var done = assert.async(),
550551
defer = jQuery.Deferred(),
551552
oldWarn = window.console.warn;
552553

553-
window.console.warn = function( msg ) {
554+
window.console.warn = function() {
554555

555556
// Support: Chrome <=41 only
556557
// Some Chrome versions newer than 30 but older than 42 display the "undefined is
557558
// not a function" error, not mentioning the function name. This has been fixed
558559
// in Chrome 42. Relax this test there.
559560
// This affects our Android 5.0 & Yandex.Browser testing.
560-
var oldChromium = false;
561+
var msg = Array.prototype.join.call( arguments, " " ),
562+
oldChromium = false;
561563
if ( /chrome/i.test( navigator.userAgent ) ) {
562564
oldChromium = parseInt(
563565
navigator.userAgent.match( /chrome\/(\d+)/i )[ 1 ], 10 ) < 42;
564566
}
565567
if ( oldChromium ) {
566-
assert.ok( /(?:barf|undefined)/.test( msg ), "Message: " + msg );
568+
assert.ok( /(?:barf|undefined)/.test( msg ), "Message (weak assertion): " + msg );
567569
} else {
568570
assert.ok( /barf/.test( msg ), "Message: " + msg );
569571
}
@@ -580,14 +582,17 @@ QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook", func
580582
// Should NOT get an error
581583
throw new Error( "Make me a sandwich" );
582584
} ).then( null, jQuery.noop )
583-
).then( function( ) {
585+
).then( function barf( ) {
586+
jQuery.thisDiesToo();
587+
} ).then( null, function( ) {
584588
window.console.warn = oldWarn;
585589
done();
586590
} );
587591

588592
defer.resolve();
589593
} );
590594

595+
// Test fails in IE9 but is skipped there because console is not active
591596
QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook with stack hooks", function( assert ) {
592597

593598
assert.expect( 2 );
@@ -605,24 +610,26 @@ QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook with s
605610
return "NO STACK FOR YOU";
606611
};
607612

608-
window.console.warn = function( msg, stack ) {
613+
window.console.warn = function() {
609614

610615
// Support: Chrome <=41 only
611616
// Some Chrome versions newer than 30 but older than 42 display the "undefined is
612617
// not a function" error, not mentioning the function name. This has been fixed
613618
// in Chrome 42. Relax this test there.
614619
// This affects our Android 5.0 & Yandex.Browser testing.
615-
var oldChromium = false;
620+
var msg = Array.prototype.join.call( arguments, " " ),
621+
oldChromium = false;
616622
if ( /chrome/i.test( navigator.userAgent ) ) {
617623
oldChromium = parseInt(
618624
navigator.userAgent.match( /chrome\/(\d+)/i )[ 1 ], 10 ) < 42;
619625
}
620626
if ( oldChromium ) {
621-
assert.ok( /(?:cough_up_hairball|undefined)/.test( msg ), "Function mentioned: " + msg );
627+
assert.ok( /(?:cough_up_hairball|undefined)/.test( msg ),
628+
"Function mentioned (weak assertion): " + msg );
622629
} else {
623630
assert.ok( /cough_up_hairball/.test( msg ), "Function mentioned: " + msg );
624631
}
625-
assert.ok( /NO STACK FOR YOU/.test( stack ), "Stack trace included: " + stack );
632+
assert.ok( /NO STACK FOR YOU/.test( msg ), "Stack trace included: " + msg );
626633
};
627634
defer.then( function() {
628635
jQuery.cough_up_hairball();

0 commit comments

Comments
 (0)