Creating your own errors

Domenic Denicola domenic at domenicdenicola.com
Wed Aug 21 09:21:38 PDT 2013


FWIW for non-DOM code I see people using `instanceof` much more often than checking `name`.

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`.

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`.


More information about the es-discuss mailing list