Closed Bug 418716 Opened 17 years ago Closed 4 years ago

JSAPI should support creating and throwing Error objects

Categories

(Core :: JavaScript Engine, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: jorendorff, Unassigned)

References

(Blocks 1 open bug)

Details

The common JS idiom "throw new TypeError('foo: name must be a string')" doesn't have a convenient JSAPI equivalent. The easiest thing to do is call JS_EvaluateScript, then JS_SetPendingException. Another option is JS_GetGlobalObject, then JS_GetProperty, then a series of API calls to create arguments, then JS_ConstructObject, then JS_SetPendingException. It would be nice to be able to do something like: if (!ok) { return JS_ThrowNewError( cx, JSEXN_TYPEERR, "foo: name must be a string"); } It would be especially nice to support all the standard error constructors, but *not* look them up dynamically, to avoid mysterious (or maybe malicious) behavior when a script clobbers them.
See also JS_GetClassObject(cx, global, JSProto_TypeError, &tyerrobj) and the like. You can get the original value of these bindings, without a property lookup if the embedding uses JSCLASS_GLOBAL_FLAGS in global's class. This is bare-bones compared to what you sketch, but I wanted to point it out in case you missed it. It is sufficient to get the constructor in order to call the constructor with the message detail arg, etc. /be
Assignee: general → nobody
Blocks: jsapi

JS::CreateError(...) in https://searchfox.org/mozilla-central/source/js/public/ErrorReport.h has been added at some point, so let's close this one.

Status: NEW → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.