Is it possible to manually throw a DOMException error in pure JavaScript? Documentation I’ve read suggests it should be relatively easy to construct (at least in Java.)
However, in Chrome, following code returns TypeError: Illegal constructor:
// DOM SYNTAX_ERR (12)
var myDOMException = new DOMException(12,"I'm sorry Dave, I'm afraid I can't do that.");
Regrettably, this is what I expected after reading the W3 docs, which don’t appear to specify a constructor at all. (As an aside, while I’m not particularly ‘au fait’ with IDL, I would have assumed their variant would support specification of constructors.)
Frustratingly, the DOMException class lurks tantalisingly in the global scope. How can I use it? Can I use it?
Update
Since I wrote this, I’ve made a couple of discoveries – namely:
var myDOMException = DOMException.constructor(12,"Error Message");
var myDOMException2 = DOMException.constructor.call(DOMException,DOMException.SYNTAX_ERR,"Error Message");
Looks like it worked!
…not so fast.
$> myDOMException instanceof DOMException
false
$> myDOMException2 instanceof DOMException
false
And possibly even more offputting:
$> myDOMException.constructor
function Number() {
[native code]
}
As always, any assistance would be greatly appreciated.
Update #2
Just to clarify my reasons for returning a DOMException object as opposed to a more generic Error – I’m trying to implement the WHATWG’s Timed Text Track spec in pure JavaScript. There are a number of instances where a proper solution would be required to return a DOMException object, specifically one with a code of 12 (SYNTAX_ERR.)
In Firefox, at least,
DOMExceptionis not a function. It is an object that defines several constants.It could be used like this:
This works if you’re trying to signal a
DOMExceptionbut don’t need an actual instance ofDOMException.Ugly, ugly hack (that basically works)
If you absolutely need an instance of
DOMExceptionthat has theSYNTAX_ERRcode, you could perform an action that causes one to be created andthrowthat:The details of the thrown exception won’t match your specific situation, of course, but the resulting object will have the correct code and pass
instanceofchecks.You could mitigate the details issue by subclassing
DOMExceptionand adding getters/setters for each of its (read-only) properties.The resulting object has the desired properties: