Debian Codegraph @

node-domexception/.history/README_20210527214323.md

raw
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 (plz don't depend on this package in any other environment other than node >=10.5)
12
13 ```js
14 import DOMException from 'node-domexception'
15 import { MessageChannel } from 'worker_threads'
16
17 async function hello() {
18 const port = new MessageChannel().port1
19 const ab = new ArrayBuffer()
20 port.postMessage(ab, [ab, ab])
21 }
22
23 hello().catch(err => {
24 console.assert(err.name === 'DataCloneError')
25 console.assert(err.code === 25)
26 console.assert(err instanceof DOMException)
27 })
28
29 const e1 = new DOMException('Something went wrong', 'BadThingsError')
30 console.assert(e1.name === 'BadThingsError')
31 console.assert(e1.code === 0)
32
33 const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
34 console.assert(e2.name === 'NoModificationAllowedError')
35 console.assert(e2.code === 7)
36
37 console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)
38 ```