Re: Constructible Exceptions

On Sunday, 25 March 2012 at 00:39, Cameron McCormack wrote:

> Marcos Caceres:
> > Yeah.. I kinda realised that after I sent the email… however…. how do you create one without first coercing an exception object out of the exception type you want? I had a go at trying to do it [1]… but I had to do the following for a DOMException:
> >  
> > try {
> > //force an exception to be generated;
> > document.removeChild({})
> > } catch (e) {
> > //use it as the prototype
> > newException = Object.create(e)
> > }
> >  
> >  
> > //alias for the prototype, helps us populate the new object
> > var proto = newException.__proto__
> >  
> > //"shadow" the name property on the new object
> > //Chrome claims these are writable, but they aint! :)
> > props = Object.getOwnPropertyDescriptor(proto, "name");
> > props.value = name;
> > Object.defineProperty(newException, "name", props);
>  
>  
>  
> I'm not really sure what you're doing here. Do you want to construct a  
> new DOMException even though the implementation does not yet allow `new  
> DOMException()`?

Yes. I'm trying to implement various specifications in ECMAScript that require me to throw a DOMException.   
> Note that you want to end up with an object whose prototype is  
> DOMException.prototype, not a DOMException instance.

Good point.   
> But before the  
> underlying DOM implementation supports constructable DOMException  
> objects you're not going to be able to create one just by using  
> Object.create, and that's because things on DOMException.prototype  
> expect the instance to be a platform exception object, and from the  
> perspective of the DOM implementation your object you have created with  
> Object.create() is not a platform exception object.

Right. But it's as close as I can get to the real thing… so again we come full circle about having some means of create a DOMException (or if faking it is good enough, which may just be).  

Received on Sunday, 25 March 2012 02:07:40 UTC