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

Brian
  • 3,264
  • 4
  • 30
  • 43
  • `to make sure I always get all the data I need to see from errors` - what do you need to see? – thefourtheye Jun 02 '15 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 02 '15 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. – thefourtheye Jun 02 '15 at 18:47

1 Answers1

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

Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
Steve H.
  • 6,912
  • 2
  • 30
  • 49
  • 12
    `Error` is actually one of the objects that `JSON.stringify` doesn't work well with. This will output just an empty object `{}`. [You'll have to manually specify properties for it to work `JSON.stringify(err,["message","arguments","type","name"])`](http://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify/26199752#26199752) – laggingreflex Jun 02 '15 at 20:37