Closed Bug 181654 Opened 22 years ago Closed 22 years ago

Calling toString for an object derived from the Error class throws TypeError

Categories

(Rhino Graveyard :: Core, defect)

defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: joerg.schaible, Assigned: norrisboyd)

Details

Attachments

(2 files)

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461) Build Identifier: Rhino 1.5 release 4 0000 00 00 (in progress) Calling the toString method for an object that is derived from the Error class results in a thrown TypeError (Rhino only, works perfectly in SpiderMonkey). Reproducible: Always Steps to Reproduce: regression test case attached Actual Results: TypeError raised Expected Results: same behaviour as with SpiderMonkey: method returns String with class name and error message. Environment is reproducable with Rhino 1.5 RC4pre from current CVS
Regression test for this bug.
Jörg's testcase added to JS testsuite: mozilla/js/tests/ecma_3/Exceptions/regress-181654.js
Confirming what Jörg has reported. SpiderMonkey passes the above test, whereas Rhino generates a TypeError. Here is an example: --------------------------- IN SPIDERMONKEY --------------------------- js> function f() {} js> f.prototype = new Error(); Error js> obj = new f(); Error js> obj.toString(); Error --------------------------- IN RHINO --------------------------- js> function f() {} js> f.prototype = new Error(); Error: undefined js> obj = new f(); js: "<stdin>", line 25: uncaught JavaScript exception: TypeError: Method "toString" called on incompatible object. (<stdin>; line 25) js> obj.toString(); js: "<stdin>", line 26: uncaught JavaScript exception: TypeError: Method "toString" called on incompatible object. (<stdin>; line 26) Note, no trouble in Rhino for a primary Error object: js> obj = new Error(); Error: undefined js> obj.toString(); Error: undefined
I guess it is possible to argue that current Rhino behavior confirms Ecma 262, v3 as according to 15.11.4.4 Error.prototype.toString() returns an implementation defined string, and throwing an exception can be very well defined result ;). But as not throwing exceptions fulfills 15.11.4.4 without guesses, here is a patch that effectively makes Error.prototype.toString a generic function printing this.name + ": " + this.message for any this.
Can be marked as fixed as I commited the attachment
Marking FIXED -
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
Marking Verified FIXED. The above testcase now passes in the Rhino shell, in both compiled and interpreted modes. Here is an interactive session: [ ] java org.mozilla.javascript.tools.shell.Main Rhino 1.5 release 4 0000 00 00 (in progress) js> function f() {} js> f.prototype = new Error(); Error: js> obj = new f(); Error: js> obj.toString() Error: js> f.prototype = new SyntaxError(); SyntaxError: js> obj = new f(); SyntaxError: js> obj.toString() SyntaxError: js> f.prototype = new TypeError(); TypeError: js> obj = new f(); TypeError: js> obj.toString() TypeError:
Status: RESOLVED → VERIFIED
Targeting as resolved against 1.5R4
Target Milestone: --- → 1.5R4
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: