Consider a script file with
let something = undefined;
let undefined = something.test;
This will throw in the Chrome console:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'test')
How can that error be sent to an Alert to the user, rather than accessing the console?
The two approaches below have been tried with no success - Also wrapping the assignment into a try catch throw.
window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
alert(
`Error: ` +
errorMsg +
` Script: ` +
url +
` Line: ` +
lineNumber +
` Column: ` +
column +
` StackTrace: ` +
errorObj
);
};
window.addEventListener("error", handleError, true);
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));
}
}
I though this might be a load order issue, but the function is in a separate script file that is loaded before the file with the incorrect assignment.