Unit_AI/node_modules/node-domexception/.history/README_20210527213345.md
2024-06-01 16:24:36 -04:00

1.5 KiB

DOMException

An implementation of the DOMException class from NodeJS

This package implements the DOMException class, from NodeJS itself. (including the legacy codes) NodeJS has DOMException built in, but it's not globally available, and you can't require/import it from somewhere.

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. This way you will have the same class that NodeJS has and you can check if the error is a instance of DOMException. 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.

import DOMException from 'node-domexception'
import { MessageChannel } from 'worker_threads'

async function hello() {
  const port = new MessageChannel().port1
  const ab = new ArrayBuffer()
  port.postMessage(ab, [ab, ab])
}

hello().catch(err => {
  console.assert(err.name === 'DataCloneError')
  console.assert(err.code === 25)
  console.assert(err instanceof DOMException)
})

const e1 = new DOMException('Something went wrong', 'BadThingsError')
console.assert(e1.name === 'BadThingsError')
console.assert(e1.code === 0)

const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
console.assert(e2.name === 'NoModificationAllowedError')
console.assert(e2.code === 7)

console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)