12

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

3
  • to make sure I always get all the data I need to see from errors - what do you need to see? Jun 2, 2015 at 18:39
  • Well, I would like to see stack trace, error message, that kind of thing. But, since I have found 3 ways to find error information, I'm left wondering what wonderful, enlightening data that is available I didn't even know about because for some reason beyond me it prints out different with different methods. In short, I don't trust that I even know about all the information that is there at this point. In short, I want it all.
    – Brian
    Jun 2, 2015 at 18:41
  • You can add arbitrary information to the error object. So, I don't think there is one-way oneliner to cover all. Jun 2, 2015 at 18:47

1 Answer 1

13

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

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.