### Install node-domexception Source: https://github.com/jimmywarting/node-domexception/blob/main/README.md Installation commands for different Node.js version requirements. ```bash # makes use of atob | require NodeJS 16+ npm install node-domexception # makes use of worker_thread | require NodeJS 10+ npm install node-domexception@1.x ``` -------------------------------- ### Import and usage of DOMException Source: https://github.com/jimmywarting/node-domexception/blob/main/README.md Examples of importing the package and verifying DOMException behavior. ```javascript import 'node-domexception' // You could also do conditional import. globalThis.DOMException || await import('node-domexception') /********************************************************************/ import { MessageChannel } from 'worker_threads' try { const port = new MessageChannel().port1 const ab = new ArrayBuffer() port.postMessage(ab, [ab, ab]) } catch (err) { console.assert(err.name === 'DataCloneError') console.assert(err.code === 25) console.assert(err.constructor === 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) ``` -------------------------------- ### Import Node DOMException Polyfill Source: https://context7.com/jimmywarting/node-domexception/llms.txt Import this module to add DOMException to the global scope. Use the conditional import to avoid loading if DOMException is already available. ```javascript import 'node-domexception' // Conditional import - only load if DOMException is not already available globalThis.DOMException || await import('node-domexception') ``` -------------------------------- ### Create Custom DOMException Instances Source: https://context7.com/jimmywarting/node-domexception/llms.txt Instantiate DOMException with a message and an optional name. Custom names map to a default code of 0, while predefined names are assigned specific legacy codes. ```javascript import 'node-domexception' // Create a custom DOMException with a custom name (code defaults to 0) const customError = new DOMException('Something went wrong', 'BadThingsError') console.log(customError.name) // 'BadThingsError' console.log(customError.message) // 'Something went wrong' console.log(customError.code) // 0 // Create a DOMException with a predefined name (gets automatic code) const noModError = new DOMException('Cannot modify', 'NoModificationAllowedError') console.log(noModError.name) // 'NoModificationAllowedError' console.log(noModError.code) // 7 ``` -------------------------------- ### Catch and Identify DOMException Errors Source: https://context7.com/jimmywarting/node-domexception/llms.txt Use 'instanceof DOMException' to reliably identify errors originating from Node.js APIs that conform to the DOMException standard. ```javascript import 'node-domexception' import { MessageChannel } from 'worker_threads' try { const port = new MessageChannel().port1 const ab = new ArrayBuffer() // Attempting to transfer the same buffer twice throws DataCloneError port.postMessage(ab, [ab, ab]) } catch (err) { if (err instanceof DOMException) { console.log(err.name) // 'DataCloneError' console.log(err.code) // 25 console.log(err.message) // Error message from Node.js } } ``` -------------------------------- ### Update package.json engines Source: https://github.com/jimmywarting/node-domexception/blob/main/README.md Configure the engines field to require Node.js 18 or higher. ```json { "engine": { "node": ">=18" } } ``` -------------------------------- ### Access Legacy DOMException Error Codes Source: https://context7.com/jimmywarting/node-domexception/llms.txt Access static properties on the DOMException class to retrieve the numerical codes associated with standard Web IDL error names. ```javascript import 'node-domexception' // Access predefined error code constants console.log(DOMException.INDEX_SIZE_ERR) // 1 console.log(DOMException.DOMSTRING_SIZE_ERR) // 2 console.log(DOMException.HIERARCHY_REQUEST_ERR) // 3 console.log(DOMException.WRONG_DOCUMENT_ERR) // 4 console.log(DOMException.INVALID_CHARACTER_ERR) // 5 console.log(DOMException.NO_DATA_ALLOWED_ERR) // 6 console.log(DOMException.NO_MODIFICATION_ALLOWED_ERR) // 7 console.log(DOMException.NOT_FOUND_ERR) // 8 console.log(DOMException.NOT_SUPPORTED_ERR) // 9 console.log(DOMException.INUSE_ATTRIBUTE_ERR) // 10 console.log(DOMException.INVALID_STATE_ERR) // 11 console.log(DOMException.SYNTAX_ERR) // 12 console.log(DOMException.INVALID_MODIFICATION_ERR) // 13 console.log(DOMException.NAMESPACE_ERR) // 14 console.log(DOMException.INVALID_ACCESS_ERR) // 15 console.log(DOMException.TYPE_MISMATCH_ERR) // 17 console.log(DOMException.SECURITY_ERR) // 18 console.log(DOMException.NETWORK_ERR) // 19 console.log(DOMException.ABORT_ERR) // 20 console.log(DOMException.URL_MISMATCH_ERR) // 21 console.log(DOMException.QUOTA_EXCEEDED_ERR) // 22 console.log(DOMException.TIMEOUT_ERR) // 23 console.log(DOMException.INVALID_NODE_TYPE_ERR) // 24 console.log(DOMException.DATA_CLONE_ERR) // 25 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.