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.

7
  • For handled exceptions, do your logic and then at the end throw your exception from catch block. Commented Mar 7, 2017 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? Commented Mar 7, 2017 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? Commented Mar 7, 2017 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. Commented Mar 7, 2017 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. Commented Mar 7, 2017 at 14:51

1 Answer 1

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.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.