Skip to content

isError returns false for DOMException instances #2

Open
@lionel-rowe

Description

@lionel-rowe

isError returns false for DOMException instances.

From what I can see, there are three reasons for this:

if (isNativeError) { // node 10+
return isNativeError(arg);
}

In NodeJS, require('util').types.isNativeError(new DOMException()) gives false.

if ($structuredClone) {
try {
return $structuredClone(arg) instanceof $Error;

In current server-side implementations (pending whatwg/webidl#1421), structuredClone(new DOMException()) returns {}, i.e. a plain JS object.

if (!hasToStringTag || !(Symbol.toStringTag in arg)) {
var str = $toString(arg);
return str === '[object Error]' // errors
|| str === '[object DOMException]' // browsers

Even if this line was hit (which it won't be because the previous two lines would both return early with false), Symbol.toStringTag in new DOMException() returns true, so the condition fails.

It doesn't seem there's any 100% foolproof way of detecting DOMExceptions until whatwg/webidl#1421 lands in implementations, so you could either:

  • Be lax with Symbol.toStringTag tag specifically in the case of DOMException*, which would fail on DOMExceptions with spoofed toStringTag or other objects with toStringTag spoofed as "DOMException"
  • Bring forward instanceof checking specifically in the case of DOMException*, which would fail on cross-realm DOMExceptions. I guess this can't be tested for in Node as require('vm').runInNewContext('new DOMException()') gives DOMException is not defined, but such cross-realm DOMExceptions are presumably pretty commonplace in browsers (e.g. any DOMException originating from an iframe)

* and maybe DOMError and/or Exception too?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions