Reputation: 543
Obviously in console core modules(https://nodejs.org/dist/latest-v4.x/docs/api/console.html) of node documentation exist below code:
console.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to stderr
but,when I run test.js which code show below
var err = new Error('a');
console.error(err);
the terminal print the message like:
Error: a at Object. (/Users/suoyong/Express/连接数据库/error.js:1:73) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.runMain (module.js:590:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:509:3
As you can see, my code is same with the node doc, and yet the result not. Please help me with the tiny question.,
Upvotes: 2
Views: 1440
Reputation: 30013
console.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to stderr
This is not to be interpreted in the literal sense. Not in the latest LTS and stable versions anyway. Printing an error like that will actually print a textual representation of the error object, which was referred as [Error: Whoops, something bad happened]
in the documentation. The actual intended behaviour is further clarified in the documentation of Console.error()
:
If formatting elements (e.g. %d) are not found in the first string then
util.inspect()
is called on each argument and the resulting string values are concatenated.
On the side of util.inspect()
, this method "returns a string representation of object that is primarily useful for debugging". For objects of type Error
, this will yield a string containing the error's message and stack trace.
> const txt = util.inspect(new Error("I'm on SO"))
undefined
> txt
'Error: I\'m on SO\n at repl:1:26\n at sigintHandlersWrap (vm.js:22:35)\n at sigintHandlersWrap (vm.js:96:12)\n at ContextifyScript.Script.runInThisContext (vm.js:21:12)\n at REPLServer.defaultEval (repl.js:313:29)\n at bound (domain.js:280:14)\n at REPLServer.runBound [as eval] (domain.js:293:12)\n at REPLServer.<anonymous> (repl.js:513:10)\n at emitOne (events.js:101:20)\n at REPLServer.emit (events.js:188:7)'
> console.log(txt)
Error: I'm on SO
at repl:1:26
at sigintHandlersWrap (vm.js:22:35)
at sigintHandlersWrap (vm.js:96:12)
at ContextifyScript.Script.runInThisContext (vm.js:21:12)
at REPLServer.defaultEval (repl.js:313:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:513:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:188:7)
Upvotes: 1
Reputation: 203359
I guess that this changed between Node v4 and Node v6.
With v4, the output is as documented; with v6, a stack trace is included in the output, like what you're seeing.
You can work around it by using console.error(err.toString())
, which for both versions will output Error: a
(so minus any brackets, but if you really want those you can add them, of course).
Upvotes: 0