Proposal: Make `await $.ajax()` throw `Error` instances instead of raw jqXHR
Gods of software development! Bring us a day when error handling in JavaScript just works!
Is your feature request related to a problem? Please describe.
When using await $.ajax(...) in modern JavaScript or TypeScript, jQuery throws the raw jqXHR object on failure, rather than a standard Error instance. This breaks consistency with native Promises and fetch-based workflows, where developers expect .catch() or try/catch to receive an Error subclass.
try {
await $.ajax({ url: '/fail' });
} catch (err) {
// err is a jqXHR object, not an instance of Error
console.log(err instanceof Error); // false
}
This behavior makes error handling, logging, and typing in TypeScript more difficult and inconsistent with common JS conventions.
Describe the solution you'd like
When $.ajax() is used with await, rejections should be wrapped in an Error instance that contains the original jqXHR as a property (e.g. err.jqXHR), or extend Error via a custom subclass like AjaxError.
class AjaxError extends Error {
constructor(jqXHR) {
super(message);
this.name = 'AjaxError';
this.jqXHR = jqXHR;
}
// repeat all available properties of jqXHR for compatibility
}
And internally:
reject(new AjaxError(jqXHR));
Describe alternatives you've considered
- Manually wrapping the
jqXHRin userlandcatchblocks - Writing a wrapper around
$.ajax()to normalize errors
While these are possible, they are not intuitive and add friction for developers using jQuery with async/await.
Additional context
Since jQuery 3.0 supports native Promise interfaces, this change would align better with modern standards and developer expectations.
Source: jquery/jquery