Sunday, 12 March 2017

Re-throwing exception in NodeJS and not losing stack trace

How can I rethrow an error or exception in nodejs/javascript and include a custom message.

I have the following code

var json = JSON.parse(result);

and I wanted to include the result content in the exception message should any parsing error occurs. Something like this.

1.  try 
2.  {
3.    var json = JSON.parse(result);
4.    expect(json.messages.length).to.be(1);
5.  } catch(ex) {
6.    throw new Error(ex.message + ". " + "JSON response: " + result);
7.  }

The problem here is that I lose my stack trace.

Is there a way to do it similar to java?

throw new Error("JSON response: " + result, ex);



via alayor

No comments:

Post a Comment