14

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? 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. 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. Commented Jun 2, 2015 at 18:47

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

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"])

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.