I'm trying to send ALL my Javascript exceptions, handled as well as unhandled ones, to server for logging. I've gone through window.onerror
documentation and also this post that throws light on this issue, but they work best for unhandled exceptions only.
window.onerror
does not fire if the exception is already handled in a catch()
block.
One 'painful' way would be to attach my logToServer()
function manually to every catch()
block in my code (and it works too), but I'm hoping for a better solution.
I've set up a little snippet below.
var logToServer = function logToServer() {
console.info('logToServer() called');
};
window.onerror = function(z, x, c, v, b) {
console.info(z);
console.info(x);
console.info(c);
console.info(v);
console.info(b);
// call a function to send to server
logToServer();
};
window.addEventListener('error', function(z, x, c, v, b) {
console.info(z);
// call a function to send to server
logToServer();
});
// test w/o catch-block
//console.info(err); // calls logToServer()
// test w/ catch-block
try {
console.info(asd); // does not call logToServer()
} catch (err) {
console.error(err);
};
Enable logs in console.