The issue has to do with the fact that some of the properties set on Errors are configured as non-enumerable.
Here's something you can use to properly stringify Error objects, it sets a toJSON()
method that JSON.stringify()
looks for when converting an object:
var config = {
configurable: true,
value: function() {
var alt = {};
var storeKey = function(key) {
alt[key] = this[key];
};
Object.getOwnPropertyNames(this).forEach(storeKey, this);
return alt;
}
};
Object.defineProperty(Error.prototype, 'toJSON', config);
Then just use JSON.stringify()
as normal.