1 | # DOMException
|
---|
2 | An implementation of the DOMException class from NodeJS
|
---|
3 |
|
---|
4 | This package exposes the [`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException) class that comes from NodeJS itself. (including all of the deprecated legacy codes)
|
---|
5 | NodeJS has it 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 since it has to re-construct the hole class from the ground up.
|
---|
10 |
|
---|
11 | ```js
|
---|
12 | import DOMException from 'node-domexception'
|
---|
13 | import { MessageChannel } from 'worker_threads'
|
---|
14 |
|
---|
15 | async function hello() {
|
---|
16 | const port = new MessageChannel().port1
|
---|
17 | const ab = new ArrayBuffer()
|
---|
18 | port.postMessage(ab, [ab, ab])
|
---|
19 | }
|
---|
20 |
|
---|
21 | hello().catch(err => {
|
---|
22 | console.assert(err.name === 'DataCloneError')
|
---|
23 | console.assert(err.code === 25)
|
---|
24 | console.assert(err instanceof DOMException)
|
---|
25 | })
|
---|
26 |
|
---|
27 | const e1 = new DOMException('Something went wrong', 'BadThingsError')
|
---|
28 | console.assert(e1.name === 'BadThingsError')
|
---|
29 | console.assert(e1.code === 0)
|
---|
30 |
|
---|
31 | const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
|
---|
32 | console.assert(e2.name === 'NoModificationAllowedError')
|
---|
33 | console.assert(e2.code === 7)
|
---|
34 |
|
---|
35 | console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)
|
---|
36 | ```
|
---|