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:
window.onerror = function (errorMsg, url, lineNumber) { alert(errorMsg + ' ' + lineNumber); };
console.error('test') - does this trigger it?
throw new Error() - or this?
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
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.