1

I know there are many other posts about this topic, but I still can't get it working. I am using Angular 8 in my project and I want to send the browser console message to a server log file. Could someone please clearifiy does questions for me:

  1. When does the window.onerror get trigged? - right know I put the function within the constructor, is that ok so? Does it get trigged, if I serve the project local?
  window.onerror = function (errorMsg, url, lineNumber) {
      alert(errorMsg + ' ' + lineNumber);
    };
  1. What do I have to do to get it triggered? (or does for example a thrown error of a missing certificate - trigger this function?)

console.error('test') - does this trigger it?

throw new Error() - or this?

  1. Does Console-Errors like CORS will trigger this function?
  2. I also read that it would get triggered if an img or script source is not avilable.

app.compentent.html: <img src="test.png">

app.copmentent.ts in the constructor (the code of point one)

But I still don't get the alert to display.

I am grateful for any help - thanks

4
  • documentation Commented Oct 9, 2020 at 8:36
  • 1
    Thanks - but I already visited this site, but there I have still a view questions. Could you please give me an explation of where to use this function? (and why should I use it - what's the benefit of it?) @Jaromanda X Commented Oct 9, 2020 at 9:22
  • @angularQuestions look at developer.mozilla.org/en-US/docs/Web/API/Window/error_event for an explanation Commented Oct 14, 2020 at 8:30
  • Will the Angular's ErrorHandler also catch errors passed to window.onerror handler? I am suggesting that you will have the error within ErrorHandler.handleError(error) - so you can log that error there. Commented Mar 5, 2024 at 13:54

2 Answers 2

1

The following may work for you. I used it in some Vue.js code to test triggering window.onerror(). Using eval() should allow you to bypass the compile-time checks of Angular and putting it in window.onload() will ensure it is evaluated after page load.

window.onerror = function (message, source, lineno, colno, error) {
  alert('onerror called: ' + message);
}

window.onload = function () {
  eval('undefined_function()');
}

As a security note, don't use eval() in production, especially if user input could get anywhere near it.

Sign up to request clarification or add additional context in comments.

Comments

0

I've tried triggering error function with your code

window.onerror = function (errorMsg, url, lineNumber) {
      alert(errorMsg + ' ' + lineNumber);
    };


callingnonexistancefunction()

It is triggering while calling a non exist function.

Hope you'll get some reference

1 Comment

Thanks for the answer - but I still can't get it working. Because in Angular it will not compile if the function is not defined. @Diwakar So is there another way to test window.onerror function?

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.