I have a SPA built with Lightning web components. My basic requirement is to catch any kind of JavaScript exceptions or errors and log them, without having to go to each inner component and wrap every piece of JS code in try catch.
I have tried using window.onerror but I am not getting the proper error message with this, it gives message as "Script error." and line no, col no as 0. Looks like some issue with same origin policy
Can any of you please suggest some other effective way to catch any kind of JavaScript errors globally ?
As far as I know, this is the only way. Could you please provide the code that you are currently using?
I'll include here the way that i had implemented it.
<script>
window.onerror = function (message, source, lineno, colno, error) {
console.log(`Error: ${error.message} on line: ${lineno} \nfull message: ${error}`)
return true;
};
function triggerError() {
x();
}
triggerError()
</script>
Output: Error: x is not defined on line: 17 full message: ReferenceError: x is not defined