0

Some of the example code in the AWS Javascript SDK docs log the stack of an error separately to the error itself, for example this is from the AWS.CloudWatch overview:

var cloudwatch = new AWS.CloudWatch();
cloudwatch.getMetricWidgetImage(params, function (err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

So far as I can tell, this is redundant in Node (I have only tested on v10.16). If I log just the error object, it includes the stack trace:

> console.log(e)
Error: x
    at repl:2:8
    at Script.runInThisContext (vm.js:122:20)
    at REPLServer.defaultEval (repl.js:332:29)
    at bound (domain.js:402:14)
    at REPLServer.runBound [as eval] (domain.js:415:12)
    at REPLServer.onLine (repl.js:642:10)
    at REPLServer.emit (events.js:198:13)
    at REPLServer.EventEmitter.emit (domain.js:448:20)
    at REPLServer.Interface._onLine (readline.js:308:10)
    at REPLServer.Interface._normalWrite (readline.js:451:12)
undefined

Whereas if I log both, as Amazon have done above, I get an ugly ouput with duplication:

> console.log(e, e.stack)
Error: x
    at repl:2:8
    at Script.runInThisContext (vm.js:122:20)
    at REPLServer.defaultEval (repl.js:332:29)
    at bound (domain.js:402:14)
    at REPLServer.runBound [as eval] (domain.js:415:12)
    at REPLServer.onLine (repl.js:642:10)
    at REPLServer.emit (events.js:198:13)
    at REPLServer.EventEmitter.emit (domain.js:448:20)
    at REPLServer.Interface._onLine (readline.js:308:10)
    at REPLServer.Interface._normalWrite (readline.js:451:12) 'Error: x\n    at repl:2:8\n    at Script.runInThisContext (vm.js:122:20)\n    at REPLServer.defaultEval (repl.js:332:29)\n    at bound (domain.js:402:14)\n    at REPLServer.runBound [as eval] (domain.js:415:12)\n    at REPLServer.onLine (repl.js:642:10)\n    at REPLServer.emit (events.js:198:13)\n    at REPLServer.EventEmitter.emit (domain.js:448:20)\n    at REPLServer.Interface._onLine (readline.js:308:10)\n    at REPLServer.Interface._normalWrite (readline.js:451:12)'
undefined

Is there some reason that Amazon are using the console.log(err, err.stack) pattern that I am missing if I just do console.log(err)?

(I am primarily using Node in an AWS Lambda, version 10.x)

Rich
  • 15,048
  • 2
  • 66
  • 119
  • Possibly a carryover from older Node.js behavior, mentioned at https://stackoverflow.com/a/33593443/1695906 – Michael - sqlbot Nov 28 '19 at 00:19
  • Thanks @Michael - sqlbot, yes, that explains it: it was required for Node < 6. If you convert your comment to an answer, I will accept it. – Rich Nov 28 '19 at 10:08

1 Answers1

0

The console.log(err, err.stack) formula was required for Node versions < 6, as they did not log the error stack by default.

More recent Node versions can just do console.log(err).

See https://stackoverflow.com/a/33593443/1695906 for a bit more detail

(The oldest Node offered by AWS Lambda at the time of writing is Node.js 8.10, so perhaps AWS should update their docs now to remove this.)

(From "@Michael - sqlbot"'s comment, thanks!)

Rich
  • 15,048
  • 2
  • 66
  • 119