DOMException
Elide implements the WebIDL-standard [DOMException][spec] class as a global, matching the shape
used by Web APIs (encoding, abort, URL) and by Node.js.
[spec]: https://webidl.spec.whatwg.org/#idl-DOMException
Constructor
js
new DOMException(message = "", name = "Error")Both arguments are optional and coerced via ToString. undefined falls back to the default.
Calling DOMException(...) without new throws TypeError.
Instance attributes
| Member | Type | Notes |
|---|---|---|
name | string | Readonly; value passed to the constructor, default "Error". |
message | string | Readonly; value passed to the constructor, default "". |
code | number | Readonly; derived from name via the legacy DOM Level 1 table, 0 if unmapped. |
name/message/code is an accessor on the prototype; stealing the accessor
(Object.getOwnPropertyDescriptor(DOMException.prototype, "name").get.call({})) throws
TypeError.
Legacy numeric code table
code is derived from name per the following mapping (names outside the table yield 0):
name | code |
|---|---|
IndexSizeError | 1 |
HierarchyRequestError | 3 |
WrongDocumentError | 4 |
InvalidCharacterError | 5 |
NoModificationAllowedError | 7 |
NotFoundError | 8 |
NotSupportedError | 9 |
InUseAttributeError | 10 |
InvalidStateError | 11 |
SyntaxError | 12 |
InvalidModificationError | 13 |
NamespaceError | 14 |
InvalidAccessError | 15 |
TypeMismatchError | 17 |
SecurityError | 18 |
NetworkError | 19 |
AbortError | 20 |
URLMismatchError | 21 |
QuotaExceededError | 22 |
TimeoutError | 23 |
InvalidNodeTypeError | 24 |
DataCloneError | 25 |
Legacy constants
Each of the 25 legacy error codes is available as a [[Writable]]=false, [[Enumerable]]=true,
[[Configurable]]=false data property on both the DOMException constructor function and the
prototype:
INDEX_SIZE_ERR…DATA_CLONE_ERR, numeric values1…25.
Inheritance
DOMException.prototype inherits from Error.prototype, so instance instanceof Error holds,
matching browsers and Node.js. toString() is inherited from Error.prototype.toString and
returns "name: message" (or just "name" when message is empty).
Example
js
const err = new DOMException("input is not valid base64", "InvalidCharacterError");
err.name; // "InvalidCharacterError"
err.message; // "input is not valid base64"
err.code; // 5
err instanceof Error; // true
err instanceof DOMException; // true
String(err); // "InvalidCharacterError: input is not valid base64"Built-in APIs that raise a spec-mandated DOMException — currently atob and btoa on bad
input — return a DOMException instance rather than a plain Error.