]> source.dussan.org Git - jquery.git/commitdiff
Deferred: Rename `getStackHook` to `getErrorHook`
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Tue, 14 Mar 2023 21:32:45 +0000 (22:32 +0100)
committerGitHub <noreply@github.com>
Tue, 14 Mar 2023 21:32:45 +0000 (22:32 +0100)
Rename `jQuery.Deferred.getStackHook` to `jQuery.Deferred.getErrorHook`
to indicate passing an error instance is usually a better choice - it
works with source maps while a raw stack generally does not.

In jQuery `3.7.0`, we'll keep both names, marking the old one as
deprecated. In jQuery `4.0.0` we'll just keep the new one. This
change implements the `4.0.0` version; PR gh-5212 implements
the `3.7.0` one.

Fixes gh-5201
Closes gh-5211
Ref gh-5212

src/deferred.js
src/deferred/exceptionHook.js
test/unit/deferred.js

index dc42947265dd75b6c69f2224c6d602ad792434e5..0d77ac9d614ee703e0b45c44c344673c13df1633 100644 (file)
@@ -188,7 +188,7 @@ jQuery.extend( {
 
                                                                                        if ( jQuery.Deferred.exceptionHook ) {
                                                                                                jQuery.Deferred.exceptionHook( e,
-                                                                                                       process.stackTrace );
+                                                                                                       process.error );
                                                                                        }
 
                                                                                        // Support: Promises/A+ section 2.3.3.3.4.1
@@ -216,10 +216,10 @@ jQuery.extend( {
                                                                process();
                                                        } else {
 
-                                                               // Call an optional hook to record the stack, in case of exception
+                                                               // Call an optional hook to record the error, in case of exception
                                                                // since it's otherwise lost when execution goes async
-                                                               if ( jQuery.Deferred.getStackHook ) {
-                                                                       process.stackTrace = jQuery.Deferred.getStackHook();
+                                                               if ( jQuery.Deferred.getErrorHook ) {
+                                                                       process.error = jQuery.Deferred.getErrorHook();
                                                                }
                                                                window.setTimeout( process );
                                                        }
index 0315b2ecbab2ec33da771c35bf41395b0c03b0d7..a5e7b5d3ca186e19a5e5db6960f710118b9eaf9c 100644 (file)
@@ -6,13 +6,16 @@ import "../deferred.js";
 // warn about them ASAP rather than swallowing them by default.
 var rerrorNames = /^(Eval|Internal|Range|Reference|Syntax|Type|URI)Error$/;
 
-jQuery.Deferred.exceptionHook = function( error, stack ) {
+// If `jQuery.Deferred.getErrorHook` is defined, `asyncError` is an error
+// captured before the async barrier to get the original error cause
+// which may otherwise be hidden.
+jQuery.Deferred.exceptionHook = function( error, asyncError ) {
 
        if ( error && rerrorNames.test( error.name ) ) {
                window.console.warn(
                        "jQuery.Deferred exception",
                        error,
-                       stack
+                       asyncError
                );
        }
 };
index 8a215ff998527a630a78faa3ee4701dd80f456db..2c791cd1c321e7685d7382c16aae1f2952213335 100644 (file)
@@ -602,7 +602,7 @@ QUnit.test( "jQuery.Deferred.exceptionHook", function( assert ) {
        defer.resolve();
 } );
 
-QUnit.test( "jQuery.Deferred.exceptionHook with stack hooks", function( assert ) {
+QUnit.test( "jQuery.Deferred.exceptionHook with error hooks", function( assert ) {
 
        assert.expect( 2 );
 
@@ -610,26 +610,26 @@ QUnit.test( "jQuery.Deferred.exceptionHook with stack hooks", function( assert )
                defer = jQuery.Deferred(),
                oldWarn = window.console.warn;
 
-       jQuery.Deferred.getStackHook = function() {
+       jQuery.Deferred.getErrorHook = function() {
 
                // Default exceptionHook assumes the stack is in a form console.warn can log,
-               // but a custom getStackHook+exceptionHook pair could save a raw form and
+               // but a custom getErrorHook+exceptionHook pair could save a raw form and
                // format it to a string only when an exception actually occurs.
                // For the unit test we just ensure the plumbing works.
-               return "NO STACK FOR YOU";
+               return "NO ERROR FOR YOU";
        };
 
        window.console.warn = function() {
                var msg = Array.prototype.join.call( arguments, " " );
                assert.ok( /cough_up_hairball/.test( msg ), "Function mentioned: " + msg );
-               assert.ok( /NO STACK FOR YOU/.test( msg ), "Stack trace included: " + msg );
+               assert.ok( /NO ERROR FOR YOU/.test( msg ), "Error included: " + msg );
        };
 
        defer.then( function() {
                jQuery.cough_up_hairball();
        } ).then( null, function( ) {
                window.console.warn = oldWarn;
-               delete jQuery.Deferred.getStackHook;
+               delete jQuery.Deferred.getErrorHook;
                done();
        } );