source: node_modules/node-domexception/.history/README_20210527213345.md@ b78c0c1

main
Last change on this file since b78c0c1 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1# DOMException
2An implementation of the DOMException class from NodeJS
3
4This package implements the [`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException) class, from NodeJS itself. (including the legacy codes)
5NodeJS has DOMException built in, but it's not globally available, and you can't require/import it from somewhere.
6
7The only possible way is to use some web-ish tools that have been introduced into NodeJS that throws an error and catch the constructor.
8This way you will have the same class that NodeJS has and you can check if the error is a instance of DOMException.
9The 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
12import DOMException from 'node-domexception'
13import { MessageChannel } from 'worker_threads'
14
15async function hello() {
16 const port = new MessageChannel().port1
17 const ab = new ArrayBuffer()
18 port.postMessage(ab, [ab, ab])
19}
20
21hello().catch(err => {
22 console.assert(err.name === 'DataCloneError')
23 console.assert(err.code === 25)
24 console.assert(err instanceof DOMException)
25})
26
27const e1 = new DOMException('Something went wrong', 'BadThingsError')
28console.assert(e1.name === 'BadThingsError')
29console.assert(e1.code === 0)
30
31const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
32console.assert(e2.name === 'NoModificationAllowedError')
33console.assert(e2.code === 7)
34
35console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)
36```
Note: See TracBrowser for help on using the repository browser.