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

154
Visualizações
Throwing user friendly error message by providing a hint

I have created a simple program that throws an error.

index.js:

const tester = require('./Tester.js');

tester("throw");

Tester.js

const error = require('./GetError.js');
module.exports = (text) => {

  if (text === "throw") {
    throw new Error("Word throw is forbidden!")
  }

}

GetError.js

module.exports = (n) => {

  if (n===1) { return  }

}

If I check the error trace I have this:

hiddenPath\Tester.js:5
    throw error(1);
    ^

Error: Word throw is forbidden!
    at module.exports (hiddenPath\GetError.js:3:23)
    at module.exports (hiddenPath\Tester.js:5:11)
    at Object.<anonymous> (hiddenPath\index.js:3:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

What I want is to tell the user where the error came from, and in this case it was when we provided the string "throw", so the output I want should be like this:

hiddenPath\index.js:3
        tester("throw")
               ^
    
    Error: Word throw is forbidden!
        at ...

I should clearly specify that the first argument that caused the error to be thrown.

An example is with jest it gives errors like this: (It clearly tells you what cause the problem)

  86 |     val = getVal(x);
  87 |
> 88 |     expect(val).toEqual("test");
     |                             ^
  89 |   });
  90 |
  91 | });

How can I achieve this?

about 4 years ago · Juan Pablo Isaza
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