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

MemberTypeNotes
namestringReadonly; value passed to the constructor, default "Error".
messagestringReadonly; value passed to the constructor, default "".
codenumberReadonly; derived from name via the legacy DOM Level 1 table, 0 if unmapped.
Each of 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):
namecode
IndexSizeError1
HierarchyRequestError3
WrongDocumentError4
InvalidCharacterError5
NoModificationAllowedError7
NotFoundError8
NotSupportedError9
InUseAttributeError10
InvalidStateError11
SyntaxError12
InvalidModificationError13
NamespaceError14
InvalidAccessError15
TypeMismatchError17
SecurityError18
NetworkError19
AbortError20
URLMismatchError21
QuotaExceededError22
TimeoutError23
InvalidNodeTypeError24
DataCloneError25

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_ERRDATA_CLONE_ERR, numeric values 125.

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.