[d24f17c] | 1 | # DOMException
|
---|
| 2 | An implementation of the DOMException class from NodeJS
|
---|
| 3 |
|
---|
| 4 | NodeJS has DOMException built in, but it's not globally available, and you can't require/import it from somewhere.
|
---|
| 5 |
|
---|
| 6 | 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 legacy codes)
|
---|
| 7 |
|
---|
| 8 | <sub>(plz don't depend on this package in any other environment other than node >=10.5)</sub>
|
---|
| 9 |
|
---|
| 10 | ```js
|
---|
| 11 | import DOMException from 'node-domexception'
|
---|
| 12 | import { MessageChannel } from 'worker_threads'
|
---|
| 13 |
|
---|
| 14 | async function hello() {
|
---|
| 15 | const port = new MessageChannel().port1
|
---|
| 16 | const ab = new ArrayBuffer()
|
---|
| 17 | port.postMessage(ab, [ab, ab])
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | hello().catch(err => {
|
---|
| 21 | console.assert(err.name === 'DataCloneError')
|
---|
| 22 | console.assert(err.code === 25)
|
---|
| 23 | console.assert(err instanceof DOMException)
|
---|
| 24 | })
|
---|
| 25 |
|
---|
| 26 | const e1 = new DOMException('Something went wrong', 'BadThingsError')
|
---|
| 27 | console.assert(e1.name === 'BadThingsError')
|
---|
| 28 | console.assert(e1.code === 0)
|
---|
| 29 |
|
---|
| 30 | const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
|
---|
| 31 | console.assert(e2.name === 'NoModificationAllowedError')
|
---|
| 32 | console.assert(e2.code === 7)
|
---|
| 33 |
|
---|
| 34 | console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)
|
---|
| 35 | ```
|
---|
| 36 |
|
---|
| 37 | # Background
|
---|
| 38 |
|
---|
| 39 | The only possible way is to use some web-ish tools that have been introduced into NodeJS that throws a DOMException and catch the constructor. This is exactly what this package dose for you and exposes it.<br>
|
---|
| 40 | This way you will have the same class that NodeJS has and you can check if the error is a instance of DOMException.<br>
|
---|
| 41 | 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.
|
---|
| 42 |
|
---|
| 43 | The DOMException is used in many places such as the Fetch API, File & Blobs, PostMessaging and more. <br>
|
---|
| 44 | Why they decided to call it **DOM**, I don't know
|
---|
| 45 |
|
---|
| 46 | Please consider sponsoring if you find this helpful
|
---|