Created
June 6, 2024 14:08
-
-
Save AbhiPrasad/6ee96da39277162b601aae1d0b3ff362 to your computer and use it in GitHub Desktop.
Monkey patch `Promise.catch`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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