Created
December 12, 2013 13:14
-
-
Save itayw/7927759 to your computer and use it in GitHub Desktop.
stringify errors.
http://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(Error.prototype, 'toJSON', { | |
value: function () { | |
var alt = {}; | |
Object.getOwnPropertyNames(this).forEach(function (key) { | |
alt[key] = this[key]; | |
}, this); | |
return alt; | |
}, | |
configurable: true | |
}); | |
var error = new Error('testing'); | |
error.detail = 'foo bar'; | |
console.log(JSON.stringify(error)); | |
// {"message":"testing","detail":"foo bar"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment