source: node_modules/node-domexception/.history/README_20210527214408.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.6 KB
Line 
1# DOMException
2An implementation of the DOMException class from NodeJS
3
4This 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)
5NodeJS has it 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 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
14import DOMException from 'node-domexception'
15import { MessageChannel } from 'worker_threads'
16
17async function hello() {
18 const port = new MessageChannel().port1
19 const ab = new ArrayBuffer()
20 port.postMessage(ab, [ab, ab])
21}
22
23hello().catch(err => {
24 console.assert(err.name === 'DataCloneError')
25 console.assert(err.code === 25)
26 console.assert(err instanceof DOMException)
27})
28
29const e1 = new DOMException('Something went wrong', 'BadThingsError')
30console.assert(e1.name === 'BadThingsError')
31console.assert(e1.code === 0)
32
33const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
34console.assert(e2.name === 'NoModificationAllowedError')
35console.assert(e2.code === 7)
36
37console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)
38```
Note: See TracBrowser for help on using the repository browser.