public
Last active

Properties of Error instances in the latest V8 included by node@0.6.6 are not enumerable. And thus cannot be serialized through JSON.stringify

  • Download Gist
v8-0412.js
JavaScript
1 2 3 4 5 6 7 8 9
> var err = new Error('wtf?');
> JSON.stringify(err)
'{"stack":"Error: wtf?\\n at [object Context]:1:11\\n at Interface.<anonymous> (repl.js:179:22)\\n at Interface.emit (events.js:64:17)\\n at Interface._onLine (readline.js:153:10)\\n at Interface._line (readline.js:408:8)\\n at Interface._ttyWrite (readline.js:585:14)\\n at ReadStream.<anonymous> (readline.js:73:12)\\n at ReadStream.emit (events.js:81:20)\\n at ReadStream._emitKey (tty_posix.js:307:10)\\n at ReadStream.onData (tty_posix.js:70:12)","message":"wtf?"}'
> Object.keys(err);
[ 'stack', 'arguments', 'type', 'message' ]
> Object.getOwnPropertyDescriptor(err, 'stack');
{ get: [Function], set: [Function], enumerable: true, configurable: true }
> process.versions.v8
'3.1.8.26'
v8-066.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13
$ node
> var err = new Error('wtf?');
undefined
> JSON.stringify(err);
'{}'
> Object.keys(err);
[ ]
> Object.getOwnPropertyDescriptor(err, 'stack');
{ get: [Function], set: [Function], enumerable: false, configurable: true }
> console.dir('wtffff fuuuuuuu');
'wtffff fuuuuuuu'
> process.versions.v8
'3.6.6.14'

I can at least confirm that it works in v0.4.12 and not in v0.6.6. That's insane and annoying.

Same behavior when installing versions of Chrome using the target V8 versions.

I had the problem in v0.6.6. Was wondering why do we have a ghost error object!

Dirty hack fix would be something like having Error.prototype.toJSON return an object with the enumerable properties

Error.prototype.toJSON = function () {
  var json =  {};
  Object.getOwnPropertyNames(this).forEach(addToJSON, this);
  return json;

  function addToJSON(name) {
    var pd = Object.getOwnPropertyDescriptor(this, name);
    pd.enumerable = true;
    Object.defineProperty(json, name, pd);
  }
}

I can confirm this is still an issue in v8 3.14.5.9 and node 0.10.15

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.