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

RESOLVED FIXED in Firefox 23

Status

()

Firefox
Developer Tools: Console
RESOLVED FIXED
6 years ago
4 years ago

People

(Reporter: David Bruant, Unassigned)

Tracking

9 Branch
Firefox 23
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

6 years ago
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.
(Reporter)

Updated

6 years ago
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
Last Resolved: 5 years ago
Component: Developer Tools → Developer Tools: Console
Resolution: --- → WORKSFORME
(Reporter)

Comment 2

5 years ago
(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 → ---
(Reporter)

Comment 3

5 years ago
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.
(Reporter)

Comment 5

5 years ago
(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.
(Reporter)

Comment 7

5 years ago
(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.
(Reporter)

Updated

4 years ago
Duplicate of this bug: 837619
Will be fixed by bug 808370.
Depends on: 808370
(Reporter)

Comment 12

4 years ago
Fixed indeed after Bug 783499.

Thanks :-)
Status: REOPENED → RESOLVED
Last Resolved: 5 years ago4 years ago
Resolution: --- → FIXED

Updated

4 years ago
Target Milestone: --- → Firefox 23
You need to log in before you can comment on or make changes to this bug.