Skip to content

Commit

Permalink
Improved safeJSONStringfy that serializes Error objects. (#154)
Browse files Browse the repository at this point in the history
* Ensure that services include web routes to they can potentialy serve websites.

* Improved safeJSONStringfy that serializes Error objects.
  • Loading branch information
cjus authored Oct 16, 2017
1 parent c07560f commit 8c9f4ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 8c9f4ce

Please sign in to comment.