【发布时间】:2020-03-23 04:24:53
【问题描述】:
AWS Javascript SDK 文档中的一些示例代码将错误堆栈与错误本身分开记录,例如来自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
});
据我所知,这在 Node 中是多余的(我只在 v10.16 上测试过)。如果我只记录错误对象,它包括堆栈跟踪:
> 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
如果我同时记录两者,就像亚马逊上面所做的那样,我会得到一个带有重复的丑陋输出:
> 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
如果我只是使用console.log(err),那么亚马逊使用console.log(err, err.stack) 模式是否有某种原因?
(我主要在 AWS Lambda 中使用 Node,版本 10.x)
【问题讨论】:
-
可能是旧 Node.js 行为的遗留问题,在 stackoverflow.com/a/33593443/1695906 中提到
-
谢谢@Michael - sqlbot,是的,这解释了它:Node
标签: node.js amazon-web-services logging