You may have noticed in our code snippets so far that we weren't using console.log(err)
or console.error(err)
. That's because starting with the Node 0.6, core error properties are not enumerable anymore, which means we won't get stack
for example.
Let's test this out and see what happens:
console.log(new Error('bad things happen'));
The preceding snippet displays the following command at the terminal:
$ node logging-errors.js [Error: bad things happen]
So, all we got was the error message, which isn't very helpful without the full stack trace. A popular logging module called bunyan
has a custom serializer for errors, so you won't encounter this problem if you use it.
Another simpler module that only does error serialization is nice-error
, which we can install with NPM. If we adjust the snippet to use this module, we get what we'd expect, namely the error properties (name
, message
, and stack
):
$ node logging-errors.js { name: 'Error', message: 'bad things happen', stack: 'Error...