Skip to content

Instantly share code, notes, and snippets.

@AbhiPrasad
Created June 6, 2024 14:08
Show Gist options
  • Save AbhiPrasad/6ee96da39277162b601aae1d0b3ff362 to your computer and use it in GitHub Desktop.
Save AbhiPrasad/6ee96da39277162b601aae1d0b3ff362 to your computer and use it in GitHub Desktop.
Monkey patch `Promise.catch`
// Please don't do this in production
// It's only really useful for debugging stuff
globalThis.Promise.prototype.catch = new Proxy(
globalThis.Promise.prototype.catch,
{
apply(target, thisArg, catchArgs) {
console.log("Promise.prototype.catch called");
catchArgs[0] = new Proxy(catchArgs[0], {
apply(target, thisArg, callbackArgs) {
// callbackArgs[0] is the error passed to the catch callback
console.log(
"Promise.prototype.catch callback called with error:\n",
callbackArgs[0]
);
// Sentry.captureException(callbackArgs[0]);
return target.apply(thisArg, callbackArgs);
},
});
return target.apply(thisArg, catchArgs);
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment