0

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.

Cheetara
  • 529
  • 1
  • 6
  • 19
  • maybe some answers in here would help https://stackoverflow.com/questions/19846078/how-to-read-from-chromes-console-in-javascript – Andrew Lohr Oct 01 '21 at 16:44
  • Some interesting thoughts, however nothing seems to want to trigger on TypeError. It's logged in the console, but can't seem to be captured anywhere else. – Cheetara Oct 01 '21 at 17:36

1 Answers1

0

You tried try catch? This is working for me.

try {
  let c = undefined.test;
} catch(error) {
  alert(error);
}

Ok I was able to get the window.onerror to work by running it on a localhost server

In one javascript file I defined the following.

window.onerror = function (errorMsg) {
    alert(errorMsg);
};

In another I caused the error with:

let c = undefined.test;

I believe the reason is CORS issue see:

When an error occurs in a script, loaded from a different origin, the details of the error are not reported to prevent leaking information

Running this code on a local python http server shows the full error message in the alert. Let me know if this works for you.

Noah Brinton
  • 119
  • 4
  • It could work independently I suppose - However I was hoping that the global window object could handle this, rather than having to have try catch blocks everywhere... – Cheetara Oct 01 '21 at 17:20
  • Hey I think I found a solution lmk if it works for you – Noah Brinton Oct 01 '21 at 18:42
  • "....Instead the error reported is "Script error."" - But here, we're getting TypeError in the browser, and nothing reported to the error handler function :/ – Cheetara Oct 04 '21 at 12:41
  • Ok. Can you share more information about where you are defining the onerror function and where you are creating the error? – Noah Brinton Oct 04 '21 at 13:40
  • The two scrips are sequentially required in the head of the document. So load order is correct The error is generated in assigment.js – Cheetara Oct 06 '21 at 11:48