简体   繁体   中英

Get stacktrace from error event

I'm implementing a global exception handler which should log the exception and send information to the server. Right now my code looks like

window.addEventListener('error', function (errorEvent) {
  var errorLine = '';
  if (errorEvent.filename) {
    var lastSlashIndex = errorEvent.filename.lastIndexOf('/');
    errorLine += errorEvent.filename.substr(lastSlashIndex + 1);
  }
  if (errorEvent.lineno) errorLine += ':' + errorEvent.lineno;
  if (errorEvent.message) errorLine += ' ' + errorEvent.message;

So I can extract location where the exception was thrown, but not full stack trace. I investigated the error event which is passed to the event listener callback, but I didn't find any information about stack trace. Is it possible to get this information from the event listener callback?

I'm interested in solution for Safari mobile and Chrome mobile, but any other information is welcome, of course.

The best thing you can do is to use a library stacktrace.js , but if you want to do it by yourself look at this link and this link for V8 . The tricky part is that you'll get different format for different browsers, so better use the library or explore it's code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM