question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error objects inside object or array are discarded during logging

See original GitHub issue

e.g. not working

   // err object was created by new Error('failed')
   winston.error('An error happened:', { error: err, bar:  "foo" });
   // => An error happened: { error: {}, bar: "foo" }
   winston.error('An error happened:', { errors: [err, err, err] });
   // => An error happened: { errors: [{},{},{}] }

works well, if top level is Error itself

winston.error('An error happened:', err);

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

2reactions
Atishay96commented, May 14, 2019

I was facing the same issue so i wrote this package, utils-deep-clone. Check it out.

P.S. I didn’t do it in winston itself because i saw the PR’s section and i guess it’s a dead project.

1reaction
jon-hallcommented, Sep 5, 2016

I got the same issue - it seems to be caused by the naive cloning done in the cycle library’s decycle function (https://github.com/dscape/cycle/blob/ab67ce90e8fa2efdb8f21074661366ec56f6a724/cycle.js#L91) - Error properties are non-enumerable and so nothing gets copied into the “clone” (it returns {}).

This is called from within winston to process the metadata on each log call here - https://github.com/winstonjs/winston/blob/de69bccf957cca2247ef529c2b66c88059adbae6/lib/winston/common.js#L142 .

It would seem like an upstream PR to cycle would be the correct fix, either to iterate non-enumerable properties or perform some other more robust form of cloning. If you just wanted to fix it for your usage, a rewriter can do the job - you have to essentially ‘pre-clone’ the error objects before they get passed to cycle.

Here’s a crude rewriter using lodash 3.x’s merge (mergeWith in 4.x) with a customiser which addresses the issue:

rewriters.push(function(level, msg, meta) {
    return _.merge({}, meta, function errorCloner(objValue, srcValue) {
        if(srcValue instanceof Error) {
            var clone = {};

            // Make sure we clone both enumerable and non-enumerable properties on errors
            Object.getOwnPropertyNames(srcValue).forEach(function(prop) {
                var value = srcValue[prop];

                clone[prop] = value && typeof value === 'object' ?
                   // Recurse for objects, to handle inner exceptions
                    _.mergeWith({}, value, errorCloner) :
                    value;
            });

            return clone;
        }
    });
});

Hopefully that might be of some use to anyone else who gets the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript console.log showing Object as Array?
I am using Firebase Auth to create users and logging the error so that I can display it to the user.
Read more >
5 Ways to Log an Object to the Console in JavaScript
Here are 5 ways to log JavaScript objects directly to the console window ... Each element in the array (or enumerable property if...
Read more >
Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Read more >
JavaScript Arrays - tips, tricks and examples - CodinGame
Arrays are just regular objects. In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and...
Read more >
Destructuring assignment - The Modern JavaScript Tutorial
Arrays allow us to gather data items into an ordered list. Although, when we pass those to a function, it may need not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found