1

Suppose something like

<script src="http://mydomain.tld/buggy.js" type="text/javascript"></script>

I get in my console

Uncaught ReferenceError: foo is not defined

I can't tell the developers to wrap the code in try/catch, but nonetheless would like handle that error on every client. I saw that overriding/extending ReferenceError is not possible.

I also tried the following (from here):

function handleError(evt) {
if (evt.message) { // Chrome sometimes provides this
  alert("error: "+evt.message +"  at linenumber: "+evt.lineno+" of file: "+evt.filename);
} else {
  alert("error: "+evt.type+" from element: "+(evt.srcElement || evt.target));
}
}
window.addEventListener("error", handleError, true);

Which pops up a message almost like I'd want to "error: Script error. at linenumer:0 of file:

Can I make it include the original error message (Uncaught ReferenceError: foo is not defined)?

user857990
  • 1,140
  • 3
  • 14
  • 29
  • You may want to look at `window.onerror`. –  Apr 12 '16 at 08:31
  • I had done that before as well, sorry I hadn't mentioned that. Used the snippet from https://danlimerick.wordpress.com/2014/01/18/how-to-catch-javascript-errors-with-window-onerror-even-on-chrome-and-firefox/ which results in `Èrror: Script error. Script : Line:0 Column:0 StackTrace:null`. Is it because it's cross domain? – user857990 Apr 12 '16 at 08:41
  • Same here. According to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror) the handler should also receive the `ReferenceError`, but in latest Chrome I got `null` as the fifth parameter. I got stuck here. – Franklin Yu Sep 26 '17 at 20:27

0 Answers0