| 1 |
# DOMException |
| 2 |
An implementation of the DOMException class from NodeJS |
| 3 |
|
| 4 |
This package implements the [`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException) class, from NodeJS itself. |
| 5 |
NodeJS has DOMException built in, but it's not globally available, and you can't require/import it from somewhere. |
| 6 |
|
| 7 |
The only possible way is to use some web-ish tools that have been introduced into NodeJS that throws an error and catch the constructor. |
| 8 |
This way you will have the same class that NodeJS has and you can check if the error is a instance of DOMException. |
| 9 |
The instanceof check would not have worked with a custom class such as the DOMexception provided by domenic which also is much larger in size. |
| 10 |
|
| 11 |
```js |
| 12 |
import DOMException from 'node-domexception' |
| 13 |
|
| 14 |
hello().catch(err => { |
| 15 |
if (err instanceof DOMException) { |
| 16 |
... |
| 17 |
} |
| 18 |
}) |
| 19 |
|
| 20 |
const e1 = new DOMException("Something went wrong", "BadThingsError"); |
| 21 |
console.assert(e1.name === "BadThingsError"); |
| 22 |
console.assert(e1.code === 0); |
| 23 |
|
| 24 |
const e2 = new DOMException("Another exciting error message", "NoModificationAllowedError"); |
| 25 |
console.assert(e2.name === "NoModificationAllowedError"); |
| 26 |
console.assert(e2.code === 7); |
| 27 |
|
| 28 |
console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10); |
| 29 |
``` |
| 30 |
|
| 31 |
## APIs |
| 32 |
|
| 33 |
This package exposes two flavors of the `DOMException` interface depending on the imported module. |
| 34 |
|
| 35 |
### `domexception` module |
| 36 |
|
| 37 |
This module default-exports the `DOMException` interface constructor. |
| 38 |
|
| 39 |
### `domexception/webidl2js-wrapper` module |
| 40 |
|
| 41 |
This module exports the `DOMException` [interface wrapper API](https://github.com/jsdom/webidl2js#for-interfaces) generated by [webidl2js](https://github.com/jsdom/webidl2js). |