In my NodeJS program, I am parsing some user JSON file.
Therefore, I use:
this.config = JSON.parse(fs.readFileSync(path));
The problem is that if the json file is not correctly formed, the error that appears looks like this:
undefined:55 }, ^ SyntaxError: Unexpected token } at Object.parse (native) at new MyApp (/path/to/docker/lib/node_modules/myApp/lib/my-app.js:30:28) ...
Since this is not very convenient for the user, I would like to throw Error indicating some user-friendly message (for example, "your configuration file is not very well formed"), but I want to save stacktrace to indicate the problem line.
In the Java world, I used throw new Exception("My user friendly message", catchedException) to have the original exception that caused this.
How is this possible in the JS world?
Anthony O.
source share