From 8c9f4ce1a3b87dd9060887b4b596d8f371ccba8b Mon Sep 17 00:00:00 2001 From: Carlos Justiniano Date: Mon, 16 Oct 2017 14:15:27 -0400 Subject: [PATCH] Improved safeJSONStringfy that serializes Error objects. (#154) * Ensure that services include web routes to they can potentialy serve websites. * Improved safeJSONStringfy that serializes Error objects. --- lib/utils.js | 17 +++++++++++++---- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 78661e0..c790a42 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -23,14 +23,23 @@ class Utils { /** * @name safeJSONStringify * @summary Safe JSON stringify - * @description Note, that this function if very different from the - * JSON.stringify function in that it won't accept non objects. * @param {object} obj - object to stringify * @return {string} string - stringified object. - * Returns undefined if the object isn't a valid object or can't be stringified */ static safeJSONStringify(obj) { - return JSON.stringify(obj); + // replaceErrors below credited to Jonathan Lonowski via Stackoverflow: + // https://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify + let replaceErrors = (key, value) => { + if (value instanceof Error) { + let error = {}; + Object.getOwnPropertyNames(value).forEach((key) => { + error[key] = value[key]; + }); + return error; + } + return value; + }; + return JSON.stringify(obj, replaceErrors); } /** diff --git a/package-lock.json b/package-lock.json index aee29f2..87d2985 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "hydra", - "version": "1.4.18", + "version": "1.4.20", "lockfileVersion": 1, "dependencies": { "acorn": { diff --git a/package.json b/package.json index 775b2a4..0f16415 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hydra", - "version": "1.4.18", + "version": "1.4.20", "license": "MIT", "author": "Carlos Justiniano", "contributors": "https://github.com/flywheelsports/hydra/graphs/contributors",