jQuery.Deferred.getErrorHook is an optional function you define. When jQuery queues a .then() callback to run asynchronously, it calls this hook (with no arguments) and keeps whatever it returns — ideally a fresh Error, which carries a stack trace of the code that set the callback up. If that callback later throws, the saved error is passed as the second argument of exceptionHook. Added in jQuery 3.7.0 as the replacement for getStackHook. Stock jQuery does not define it.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
jQuery.Deferred.getErrorHook = function () {
return new Error("Deferred.then() queued here");
};
jQuery.Deferred.exceptionHook = function (error, asyncError) {
console.log(asyncError.stack); // stack captured by getErrorHook
};
// In stock jQuery:
typeof jQuery.Deferred.getErrorHook → "undefined"
Call
What it does
getErrorHook = function () { return new Error() }
Install the hook so every queued .then() callback records an Error.
exceptionHook(error, asyncError)
Receives the saved Error as asyncError when the callback throws.
typeof jQuery.Deferred.getErrorHook
"undefined" until you (or a debug tool) define it.
getErrorHook = undefined
Remove the hook to stop recording stacks.
This hook is for debugging: it creates an Error for every queued .then() callback, which costs time and memory.
Parameters
Parameter
Description
(none)
jQuery calls getErrorHook() with no arguments. You do not pass anything; you assign a function to the property.
Return Value
Return Value
Description
Error (recommended)
An Error instance created where the callback was queued. Its .stack helps source maps and dev tools show the real origin.
Any value
jQuery stores whatever you return and hands it to exceptionHook as asyncError. If the hook is not defined, asyncError is undefined.
jQuery Examples
Example 1 — Receive asyncError in the Hook
Define getErrorHook and read its Error inside exceptionHook.
jQuery
jQuery.Deferred.getErrorHook = function () {
return new Error("queued");
};
jQuery.Deferred.exceptionHook = function (error, asyncError) {
console.log(error.message); // "boom"
console.log(asyncError instanceof Error); // true
console.log(asyncError.message); // "queued"
};
$.Deferred().resolve().then(function () {
throw new Error("boom");
});
Check.typeof jQuery.Deferred.getErrorHook is "undefined" by default.
Throw. The callback still throws and reaches exceptionHook.
Missing. No hook was defined, so asyncError is undefined.
Example 3 — Enable It Only While Debugging
Install the hook behind a flag so production pages skip the extra cost.
jQuery
var DEBUG = true; // set to false in production builds
if (DEBUG) {
jQuery.Deferred.getErrorHook = function () {
return new Error("then() queued here");
};
}
console.log("getErrorHook installed: " +
(typeof jQuery.Deferred.getErrorHook === "function")); // true
Install. Only when debugging does jQuery start recording stacks.
Verify. Check the property with typeof to confirm it is active.
Frequently Asked Questions
It is an optional hook, not defined by stock jQuery. If you define it, jQuery calls it (with no arguments) when it queues a .then() callback to run asynchronously, and keeps the return value. When that callback throws, the saved value is passed to jQuery.Deferred.exceptionHook as its second argument.
Because .then() callbacks run asynchronously, a thrown error's own stack no longer shows the code that set up the chain. getErrorHook captures an Error at queue time, so asyncError.stack points back to where .then() was called, which makes async bugs far easier to trace.
A new Error instance, for example return new Error("Deferred queued"). The Error's stack property records the call site and works well with browser source maps. jQuery passes whatever you return straight through, so a string also works but an Error is recommended.
getStackHook is the older name and is deprecated. The name suggested a stack string, but jQuery just passes the value along, and an Error instance cooperates better with source maps. getErrorHook was added in jQuery 3.7.0, and when both exist jQuery uses getErrorHook.
Yes. Creating an Error for each queued .then() callback captures a stack trace, which is relatively expensive. Enable it only in development or while debugging, and remove it for production.
Stock jQuery only calls it if it exists; it never defines it. Debug helpers, such as jQuery Migrate in its development build, can install it to improve error messages. You can also define it yourself.