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?