Closed Bug 688400 Opened 13 years ago Closed 11 years ago

Web Console throws a TypeError when trying to display objects with no toString method

Categories

(DevTools :: Console, defect)

9 Branch
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED
Firefox 23

People

(Reporter: bruant.d, Unassigned)

References

Details

In latest Nightly:
< Object.create(null)
> TypeError: aResult.toString is not a function

In this case, one could rather expect to see an empty object. Also, "aResult" doesn't refer to anything I have defined.
OS: Linux → All
Hardware: x86_64 → All
Version: unspecified → 9 Branch
we now get the somewhat more descriptive:

> TypeError: can't convert null to primitive type

I think this is OK.
Status: NEW → RESOLVED
Closed: 12 years ago
Component: Developer Tools → Developer Tools: Console
Resolution: --- → WORKSFORME
(In reply to Rob Campbell [:rc] (:robcee) from comment #1)
> we now get the somewhat more descriptive:
> 
> > TypeError: can't convert null to primitive type
> 
> I think this is OK.
This is not ok. Object.create(null) has no reason to throw an error at all!

Object.create(null) returns an object with null as its prototype, no error. See ECMAScript 5 15.2.3.5
Status: RESOLVED → REOPENED
Resolution: WORKSFORME → ---
I bet that the console.log implementation, when trying to display an object, does something like:

    object.toString()

This cannot work for objects with null as [[Prototype]], because they don't have a toString method I guess (hence the TypeError)

What you need is probably to go for:

    Object.prototype.toString.call(object)

If I'm wrong, please post a link to the console.log code I'll see where I think console.log is wrong.
This kind of bug is now easy to debug with the Browser Debugger. Make sure you have the following prefs set first:

devtools.debugger.remote-enabled: true
devtools.chrome.enabled: true

Now open the Browser Debugger from the Web Developer menu and toggle the 'Pause on exceptions' setting in the gear menu.

If you then follow the STR from comment 0, the debugger will pause in the exact spot where the TypeError is thrown.
(In reply to Panos Astithas [:past] from comment #4)
> This kind of bug is now easy to debug with the Browser Debugger.
HOLY COW! THIS IS SO AWESOME!

> If you then follow the STR from comment 0, the debugger will pause in the
> exact spot where the TypeError is thrown.
It doesn't seem easy to figure out the issue, but I'll investigate.

Thanks for the tip !
David: thanks for your bug report. As far as I know, the code tries a few tricks before giving up. The exception is displayed as a placeholder for something we can't display / can't convert to a string. I could just display null, but that would be the same as displaying null - which would cause confusion.

I agree the TypeError exception is far from ideal. We could go for <cannot convert to to string> or <no string> or just [object Object]. Ideas are welcome.
(In reply to Mihai Sucan [:msucan] from comment #6)
> I agree the TypeError exception is far from ideal. We could go for <cannot
> convert to to string> or <no string> or just [object Object]. Ideas are
> welcome.
Object.create(null) is an object, so "{}" would be the most relevant thing to print or at least, the same thing than console.log({}) (which prints [object Object]).

Likewise,
  var o = Object.create(null); o.a = 1; console.log(o)
should logs the same than
  console.log({a:1});
In both case, a clickable {a:1} is fine.

Unfortunately, the console currently doesn't help to distinguish the [[Prototype]]. One idea would be that in the object popup, a link could open a popup for the prototype. I'm filing another bug for this.
(In reply to David Bruant from comment #7)
> Unfortunately, the console currently doesn't help to distinguish the
> [[Prototype]]. One idea would be that in the object popup, a link could open
> a popup for the prototype. I'm filing another bug for this.

Bug 794823 will fix that.
(In reply to Panos Astithas [:past] from comment #8)
> (In reply to David Bruant from comment #7)
> > Unfortunately, the console currently doesn't help to distinguish the
> > [[Prototype]]. One idea would be that in the object popup, a link could open
> > a popup for the prototype. I'm filing another bug for this.
> 
> Bug 794823 will fix that.

Actually, the followup bug 808370 is the one to follow.
Will be fixed by bug 808370.
Depends on: 808370
Fixed indeed after Bug 783499.

Thanks :-)
Status: REOPENED → RESOLVED
Closed: 12 years ago11 years ago
Resolution: --- → FIXED
Target Milestone: --- → Firefox 23
Product: Firefox → DevTools
You need to log in before you can comment on or make changes to this bug.