Closed Bug 894446 Opened 11 years ago Closed 11 years ago

Suggestion: improve the default string serialization (toString) of Error objects

Categories

(Core :: JavaScript Engine, enhancement)

x86
All
enhancement
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: getify, Unassigned)

Details

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20130713 Firefox/24.0 (Nightly/Aurora)
Build ID: 20130713004004

Steps to reproduce:

Run this code, like in the console.

```
function blah() {
   function foo() {
      var a = true;
      try { a(); }
      catch (err) { console.log(err+""); }
   }
   foo();
}

blah();
```


Actual results:

Outputs: "TypeError: boolean is not a function" but omits the stack trace info that was available on the error object. The stack trace is often very helpful.


Expected results:

It would be very nice if the default toString() of Error objects also included Firefox's `.stack` property from the Error instance.

I've created a simple hopefill/polyfill snippet for what that could/would look like:

https://gist.github.com/getify/6009932

-------------

Both Chrome and Firefox track this very helpful `.stack` property, so it'd be nice if both browsers outputted it by default. My hopefill/polyfill normalizes the behavior between Chrome and Firefox.
BTW, I filed the exact same suggestion over at Chrome's bug tracker: https://code.google.com/p/chromium/issues/detail?id=260740
Assignee: nobody → general
Severity: normal → enhancement
Component: Untriaged → JavaScript Engine
OS: Mac OS X → All
Product: Firefox → Core
Also, of note, the JS specification leaves the implementation of Error.prototype.toString up to browser vendors, so that's not a blocker to making changes as I propose. Of course, I filed this with both FF and Chrome because I'd like to see all browsers provide this better output by default rather than having to work harder to get it. :)
I'm not sure what JS spec you're looking at.  ES5 specifies a precise algorithm for Error.prototype.toString, which if memory serves we follow exactly.

http://es5.github.io/#x15.11.4.4
Status: UNCONFIRMED → RESOLVED
Closed: 11 years ago
Resolution: --- → INVALID
Ah, I see. I was asserting that based on MDN's page for `Error.prototype.toString`:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString

"According to ECMA-262, implementations are free to decide the behavior of this method."

I guess this page hasn't been updated since ES5 specified the behavior.

I'll take my suggestion to ES instead.
You need to log in before you can comment on or make changes to this bug.