diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js index 0c093cf..60d9bc9 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,13 @@ -Object.defineProperty(Error.prototype, 'toJSON', { - value: function () { - var alt = {}; +const fclone = require('fclone'); + - Object.getOwnPropertyNames(this).forEach(function (key) { - alt[key] = this[key]; - }, this); +if ('toJSON' in Error.prototype) { + return; +} - return alt; +Object.defineProperty(Error.prototype, 'toJSON', { + value: function () { + return fclone(this); }, configurable: true, writable: true diff --git a/package.json b/package.json index e23853e..a3cf94c 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,8 @@ "bugs": { "url": "https://github.com/AVVS/error-tojson/issues" }, - "homepage": "https://github.com/AVVS/error-tojson" + "homepage": "https://github.com/AVVS/error-tojson", + "dependencies": { + "fclone": "^1.0.11" + } }