Brian
Brian

Reputation: 3334

Output full error object in node.js

There are several places where error objects are used, like when you catch errors or in the case of exec an error object can be passed back by a child process. When you attempt to log that information, not quite all of it makes it out.

I've tried the following:

console.log(error);
console.log(error.stack);
console.log(util.inpect(error, true, null));

All these options seem to output a different set of incomplete data. Is there a one line way to make sure I always get all the data I need to see from errors displayed or do I need to use all three of these lines (are there even more statements I need to add?)?

Upvotes: 14

Views: 10584

Answers (1)

Steve H.
Steve H.

Reputation: 6947

You can convert many JavaScript objects to strings using JSON:

console.log(JSON.stringify(error, ["message", "arguments", "type", "name"]));

EDIT: Updated to reflect the point made by @laggingreflex in the comment...

Upvotes: 14

Related Questions