I'm trying to detect when a console error occurs. I've looked here and have seen 2 suggestions
window.onerror
and
try catch
The following demonstrates an issue where this is not "caught" (by caught, as per the code below, I'd expect to see an alert and I do not).
window.onerror = function(error) {
alert("window error..." + error );
}
const arr = [];
try{
arr.push({
'firstValue':'Me 'And' You',
'otherValue':5
});
}catch(err){
alert("Error..." + err);
}
The part causing the issue is
'firstValue':'Me 'And' You',
The reason for this is how this line of code is generated and I suspect the real fix is to correct this (using MVC.NET Razor)
'firstValue': '@Model.MyStringWithNoFormattingOrChecking'
Where the value of MyStringWithNoFormattingOrChecking is Me 'And' You
However, my question is about why the onerror or try catch didn't work. Or what I could have done to have caught this using javascript (I don't actually show an alert, I log via Ajax)
'Me ''within the string. I've never used MVC.NET, so I can't say how to fix the creation of the string - but I'd look there first instead of on the js part