Skip to content
Advertisement

How to specify a “caused by” in a JavaScript Error?

In my NodeJS program, I parse some user JSON file.

So I use :

JavaScript

The problem is that if the json file is not correctly formated, the error thrown is like:

JavaScript

As it is not really user friendly I would like to throw an Error specifying some user friendly message (like “your config file is not well formated”) but I want to keep the stacktrace in order to point to the problematic line.

In the Java world I used throw new Exception("My user friendly message", catchedException) in order to have the original exception which caused that one.

How is it possible in the JS world?

Advertisement

Answer

What I finally did is:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement