0

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.
Community
  • 1
  • 1
rodiwa
  • 1,690
  • 2
  • 17
  • 35
  • For handled exceptions, do your logic and then at the end throw your exception from catch block. – Muhammad Qasim Mar 07 '17 at 10:55
  • why? Either you use *try ... catch* to deal with errors you can not avoid, then these errors are dealt with *(and there shouldn't be that many places such errors occur)*. Or you use *try .. catch* everywhere to deal with crappy/buggy code and avoid the user to see these errors or their consequences. In this case, stop catching these errors and fix the code that causes them. So again, why do you have so many *try ... catch* blocks in your code that you find it cumbersome to manually add that logging? And why do you want to log (serverside) every Error that has already been dealt with? – Thomas Mar 07 '17 at 11:11
  • @MuhammadQasim - Do you mean to edit every catch block manually, and to call my logic when an exception is thrown? I can do that but to visit every catch block seems an overkill. Not to mention to always ensure that any new try catch blocks in the future should always call my server-logging-logic-function. Did you mean anything else? – rodiwa Mar 07 '17 at 14:45
  • @Misaal - window.onerror fires only when an exception is thrown. If you catch any exception, this event wont fire. You will have to do it explicitly in all try--catch blocks. See my answer. – Muhammad Qasim Mar 07 '17 at 14:49
  • @Thomas - You probably went a little out of context here. It's a specific requirement to send all handled as well as unhandled exceptions to the server. Everything you said probably makes sense (I still don't agree completely though), but that's not what I was discussing here. – rodiwa Mar 07 '17 at 14:51
  • @MuhammadQasim - I guess that's the only way to go for now. I'll still keep looking around for an alternate. Thanks, mate. – rodiwa Mar 07 '17 at 14:52
  • @Misaal - I think there is no such alternate. If you find any, that must be tedious and complex and will not serve better. However if you find any better way, do share with us. And no problem. mark my answer if its helpful – Muhammad Qasim Mar 07 '17 at 14:54

1 Answers1

0

window.onerror fires only when an exception is unhandled. If you catch any exception, this event wont fire. You will have to do it explicitly in all try--catch blocks. In catch block you will have to throw your exception. The syntax is simple:

try{
    //Your try logic
}
catch(ex){
   // Your catch logic comes here
   throw ex; //This will throw exception and window.onerror will be fired.
}
Muhammad Qasim
  • 1,622
  • 14
  • 26