From 10faafd13853da8549a1bd9625208e5f026eb203 Mon Sep 17 00:00:00 2001 From: Jonathan Lonowski Date: Sun, 3 Apr 2016 20:35:55 -0500 Subject: [PATCH 1/4] Avoid replacing existing `toJSON` definitions. --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index d92103f..8f54650 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +if (!('toJSON' in Error.prototype)) Object.defineProperty(Error.prototype, 'toJSON', { value: function () { var alt = {}; From f78f10f3a8548c2e085aa6c9baab9151ddc26ae0 Mon Sep 17 00:00:00 2001 From: Jonathan Lonowski Date: Mon, 4 Apr 2016 21:43:41 -0500 Subject: [PATCH 2/4] Revise to exit module if `toJSON` is already defined. AVVS/error-tojson#3 --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8f54650..b4af5b7 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,7 @@ -if (!('toJSON' in Error.prototype)) +if ('toJSON' in Error.prototype) { + return; +} + Object.defineProperty(Error.prototype, 'toJSON', { value: function () { var alt = {}; From 5368a1dff30ddd798ff652156854ef402ba555da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Fri, 6 Apr 2018 11:17:24 +0200 Subject: [PATCH 3/4] Use only enumerable properties --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a029ce9..6a880d5 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ Object.defineProperty(Error.prototype, 'toJSON', { value: function () { var alt = {}; - Object.getOwnPropertyNames(this).forEach(function (key) { + Object.keys(this).forEach(function (key) { alt[key] = this[key]; }, this); From 1905c804eb3013c2fa75281a55c1803e982adf2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Fri, 15 Jun 2018 09:47:35 +0200 Subject: [PATCH 4/4] Use `fclone` to do serialization --- .gitignore | 1 + index.js | 11 ++++------- package.json | 5 ++++- 3 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 .gitignore 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 6a880d5..60d9bc9 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,13 @@ +const fclone = require('fclone'); + + if ('toJSON' in Error.prototype) { return; } Object.defineProperty(Error.prototype, 'toJSON', { value: function () { - var alt = {}; - - Object.keys(this).forEach(function (key) { - alt[key] = this[key]; - }, this); - - return alt; + 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" + } }