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 inplete 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?)?
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 inplete 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?)?
Share Improve this question asked Jun 2, 2015 at 18:23 BrianBrian 3,3345 gold badges33 silver badges44 bronze badges 3-
to make sure I always get all the data I need to see from errors
- what do you need to see? – thefourtheye Commented 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 Commented 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. – thefourtheye Commented Jun 2, 2015 at 18:47
1 Answer
Reset to default 14You 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 ment...