Skip to content

Commit

Permalink
[Winston 2.x] Decycle circular Error instances (#1307)
Browse files Browse the repository at this point in the history
* Adds support for Error instances with circular references

* Bumps version to 2.5.0

* Removes superfluous line break

* Update package.json
  • Loading branch information
jamesseanwright authored and indexzero committed May 11, 2018
1 parent d9304b8 commit 292c2be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ exports.clone = function (obj) {
copy[key] = obj[key];
});

return copy;
return cycle.decycle(copy);
}
else if (!(obj instanceof Object)) {
return obj;
Expand Down Expand Up @@ -145,7 +145,7 @@ exports.log = function (options) {
: exports.timestamp,
timestamp = options.timestamp ? timestampFn() : null,
showLevel = options.showLevel === undefined ? true : options.showLevel,
meta = options.meta !== null && options.meta !== undefined && !(options.meta instanceof Error)
meta = options.meta !== null && options.meta !== undefined
? exports.clone(options.meta)
: options.meta || null,
output;
Expand Down Expand Up @@ -244,10 +244,6 @@ exports.log = function (options) {
: options.message;

if (meta !== null && meta !== undefined) {
if (meta && meta instanceof Error && meta.stack) {
meta = meta.stack;
}

if (typeof meta !== 'object') {
output += ' ' + meta;
}
Expand Down
17 changes: 17 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ helpers.testLevels = function (levels, transport, assertMsg, assertFn) {
circmetadatatest[assertMsg] = assertFn;
tests['when passed circular metadata'] = circmetadatatest;

var circerror = new Error("message!");
var foo = {};
var circerrordatatest;

foo.bar = foo;
circerror.foo = foo;
circerror.stack = 'Some stacktrace';

circerrordatatest = {
topic: function () {
transport.log('info', 'test message', circerror, this.callback.bind(this, null));
}
};

circerrordatatest[assertMsg] = assertFn;
tests['when passed circular error as metadata'] = circerrordatatest;

return tests;
};

Expand Down

0 comments on commit 292c2be

Please sign in to comment.