Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!