I want to extend the general catch prototype of the Promise object, so that I can automatically log the error to out application monitoring whenever a catch block is hit. But I have trouble getting the error object out of the Promise object when trying to extend the catch.
So basically instead of doing this in every then().catch()
axios.get('sample/url')
.then(response => { stuff })
.catch(error => {
newrelic.noticeError(error);
});
I want to extend the Promise prototype, but fail to get the Error Object out of it.
(function (Promise) {
const originalCatch = Promise.prototype.catch;
Promise.prototype.catch = function () {
console.log('> > > > > > called .catch on %o with arguments: %o', this, arguments);
if (typeof newrelic !== 'undefined') {
newrelic.noticeError(arguments[0]);
} else {
console.error(arguments);
}
return originalCatch.apply(this, arguments);
};
})(Promise);