Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda