Skip to content

Commit

Permalink
Don't swallow Error message/stack when using formatter
Browse files Browse the repository at this point in the history
Fixes winstonjs#1178

When we use a formatter and pass an `Error` object in as `meta`,
we end up removing the Error's message and stack information when
`decycle`ing it. `clone` checks if the passed-in object is an `Error`
and does not `decycle` it in that case, but when we use a formatter,
this check does not happen because we `clone` the whole `options` object
instead of just cloning `options.meta`.

By `clone`ing `meta` alone when it is an Error,
the `instanceof Error` check will catch it and copy `message` and `stack`
into a new, non-Error object. So when that's buried in `options.meta`,
re-`clone`ing it won't hurt.
  • Loading branch information
Evan Battaglia committed Jan 25, 2018
1 parent ffe883e commit ca96f55
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ exports.log = function (options) {
//
if (typeof options.formatter == 'function') {
options.meta = meta || options.meta;
if (options.meta instanceof Error) {
// Force converting the Error to an plain object now so it
// will not be messed up by decycle() when cloning options
options.meta = exports.clone(options.meta);
}
return String(options.formatter(exports.clone(options)));
}

Expand Down

0 comments on commit ca96f55

Please sign in to comment.