Javascript - where are the name and message properties of Error object?
Asked Answered
J

2

8

Trying to understand why the two cross-browser properties of Javascript Error object, namely "name" and "message", can't be found using the "for ... in" method

// error code
...
}catch( err ){

  // in FF this lists 3 properties for fileName, lineNumber and columnNumber...
  // but NOT name or message!
  for(var propertyName in err) {
    $( '#diags' ).append( 'err property: ' + propertyName + ', 
       value: ' + err[ propertyName ] + '<br>' );
  }
  // this line prints fine:
  $( '#diags' ).append( 'Error - name:' + err.name + ', message: ' + err.message + '<br>' );
}

Edit

I am asked what is name and message. These are properties (are they though?) which all Errors have in any browser... so in the above code I have added an extra line of code which shows that these "attributes" or whatever they are print fine

Edit2

Following Mati's helpful answer I did a bit of searching. This seems to answer the "inspection" question: Is it possible to get the non-enumerable inherited property names of an object?

Jacklyn answered 27/3, 2013 at 9:20 Comment(3)
What is name or message?Beware
Javascript Error object has always been a bit of a mess in some browsers. It should have a .message property. Anything else is proprietary. This article shows how the Error object can be standardized - see section titled "Custom Error Types".Upspring
@Upspring Yes I had found that page... hence my surprise that these two "properties" (if that is what they are) are not actually listed using the for ... in loop above... I remain puzzledJacklyn
S
8

A for...in loop does not iterate over non–enumerable properties.

var e = new Error('a');

console.log(e.propertyIsEnumerable('name'));
console.log(e.propertyIsEnumerable('message'));
console.log(e.propertyIsEnumerable('fileName'));
console.log(e.propertyIsEnumerable('lineNumber'));
console.log(e.propertyIsEnumerable('columnNumber'));

for(var prop in e)
{
    console.log(prop + ': ' + e[prop]);
}

Output

false
false
true
true
true
fileName: index.html
lineNumber: 25
columnNumber: 0
Singlephase answered 27/3, 2013 at 17:59 Comment(7)
Thanks! Now the question you've all been waiting for: how do I find what non-enumerable properties there are? I know, I know: they're non-enumerable, so you can't enumerate them! Is this the answer? In languages with which I'm more familiar there are usually options to "inspect" an object ... anything like that in JS?Jacklyn
@mike: see the answers at this question. (I suppose you've already figured this out -- but for those who end up here nowadays, like me, it may be still useful)Rondi
But this begs the question - "Why on earth is the Error.message property non-enumerable??" If I look at the Javascript specification at Mozilla it doesn't say so. This seems screwy!!Pressley
@Pressley Actually it says so in the beginning '/---/ for...in statement iterates over the enumerable properties of an object /---/'. Why? See relevant discussion here: #14985033Singlephase
@Singlephase - I know what it does. But that still doesn't explain why you would want to NOT iterate over the message property of an Error object. That seems like something you'd WANT to be iterable/enumerable (i.e. in a for loop). What was the reasoning behind making that field non-enumerable for for loops etc?Pressley
@Pressley I agree, message and and name ARE st you might want to show up in enum ops. I cant think of anything better than Its just seems to be the common characteristic for built in objects. I've been looking into the subject and have come to understanding that enumerability in JS is a rather idiosyncratic property.Singlephase
Is there any official or near-official documentation that lists lineNumber and other semi-standard properties of the error class?Mcconnell
M
-1

Only name and message are standard. Filenames and line numbers of errors can be obtained by parsing the string returned by new Error().stack .

Mcconnell answered 28/8 at 18:38 Comment(2)
OP knows that name and message are standard. They didn't ask for filenames and line numbers.Branson
An answer is permitted to provide additional useful information. In this case, I explained how the filenames and line numbers of errors can be obtained. Who knows? Maybe this will help the OP. It may well help others who visit here.Mcconnell

© 2022 - 2024 — McMap. All rights reserved.