Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
How to send Send TypeErros in Chrome console to Alert

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda