Why can Errors not be stringified?
JSON.stringify(new ReferenceError('foo')); // {}
When for example, Date does something more useful:
JSON.stringify(new Date()); // "2015-04-01T10:23:24.749Z"
Why can Errors not be stringified?
JSON.stringify(new ReferenceError('foo')); // {}
When for example, Date does something more useful:
JSON.stringify(new Date()); // "2015-04-01T10:23:24.749Z"
JavaScript Error
objects are not enumerable. You can verify this easily:
new Error('Test').propertyIsEnumerable('message');
// -> false
You can however define your own toJSON
function on the error Object:
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
return {value: "Test"};
},
configurable: true
});
JSON.stringify(new Error());
-> "{value: "Test"}"