NO_INSTANCEOF_ERROR
Conformance is available on Enterprise plans
A common pattern for checking if an object is an error is to use .
This pattern is problematic because errors can come from other realms. Errors from other realms are instantiated from the realm's global constructor, and are therefore not instances of the current realm's global constructor and will not pass the check.
Some examples of where you might hit this include:
- In Node.js, errors from a workers are instances of from the worker's global environment.
- In browser environments, errors from are instances of from the 's global environment (i.e. ).
By default, this rule is disabled. To enable it, refer to customizing Conformance.
In this example, an error is returned from a context. As this error was created in a different realm, returns false.
You can use in Node.js environments, which will return for errors from other realms.
Use a library like to ensure you cover errors from other realms.
You can also use in some cases. This method will not work for custom errors, and you'll need to traverse the prototype chain (i.e. )to handle those cases.
The following code is a simplified version of the code used in the library:
Was this helpful?