user2879704
user2879704

Reputation:

console.log formats error object different from Error.prototype.toString

I am seeing the Error.prototype.toString implementation here.

It mostly prints error.name and error.message as name + ': ' + msg.

But when i pass Error object to console.log, I am seeing the file not exists error prints more properties as errno, code, syscall etc.

What does console.log invoke, to print the string summary of an Error object?

Code:

var fs = require('fs')

fs.readFile('/abcd', 'utf8', function(err, res){
  console.log("the error toString method shows, " + err)
  console.log("the console log's string summary is,")
  console.log(err)
})

Output:

the error toString method shows, Error: ENOENT: no such file or directory, open '/abcd'
the console log's string summary is,
{ [Error: ENOENT: no such file or directory, open '/abcd'] errno: -2, code: 'ENOENT', syscall: 'open', path: '/abcd' }

Upvotes: 0

Views: 287

Answers (1)

Domenic
Domenic

Reputation: 112897

console.log is unspecified, so each browser implements something different. (As does Node.js.) If you want to see exactly what, you can look at the browser source code, except for IE/Edge of course.

In Node.js, which I assume you are using because of the require, the code is:

Upvotes: 2

Related Questions