Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)?

like image 745
Brian Avatar asked Feb 12 '26 17:02

Brian


1 Answers

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...

like image 124
Steve H. Avatar answered Feb 14 '26 06:02

Steve H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!