Creating your own errors
Anne van Kesteren
annevk at annevk.nl
Thu Aug 22 02:26:40 PDT 2013
On Wed, Aug 21, 2013 at 5:21 PM, Domenic Denicola
<domenic at domenicdenicola.com> wrote:
> FWIW for non-DOM code I see people using `instanceof` much more often than checking `name`.
In public-script-coord and elsewhere people have argued that
instanceof is an indication of badness:
> Thus I personally think that creating a bunch of new `DOMException` subtypes would be the way to go, e.g. a `PermissionDeniedError` whose `.name` is `PermissionDeniedError` and whose `.__proto__` is `DOMException`.
That seems like a lot of unnecessary bloat for something that's rather trivial.
> If we were starting from scratch we'd probably just have a single `DOMException` (probably named `DOMError` actually) with a `.code` property that can vary between types of errors. But since we're not, creating a large hierarchy seems best, since it allows you to preserve all the usual properties of JS errors. The only downside of it is that it creates a large hierarchy, which seems like less of a downside than e.g. having an error whose `.name` is not equal to its `.constructor.name`.
Allen suggested having .subname. But I'm not sure we can still do that
and it also seems silly to make these exceptions second-class
More information about the es-discuss