Why can’t I see the keys of an Error object?

JavaScript properties may be non-enumerable, which means they does not appear in for..in loops or Object.keys results.

You can use Object.getOwnPropertyNames to get all properties (enumerable or non-enumerable) directly on an object. I say “directly” because normal enumeration looks up the object’s prototype chain to get enumerable properties on parent prototypes, while getOwnPropertyNames does not.

Thus, Object.getOwnPropertyNames(err) only shows

['stack',
 'arguments',
 'type',
 'message']

The name property is a non-enumerable property of Error.prototype and is never set directly on an Error instance. (Prototyping recap: when you try to access err.name, the lookup err turns up nothing, so the interpreter looks at Error.prototype, which does have a name property.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)