Error.isError() in JavaScript: A Reliable Error Check

Why the new Error.isError() method solves important cross-realm issues and provides more reliable error identification than instanceof

JavaScript’s error handling system has long had a blind spot when dealing with errors across different execution contexts. The new Error.isError() method addresses this limitation, providing developers with a more reliable way to identify error objects.

The traditional approach to checking if a value is an Error has been using the instanceof operator.

JavaScript
try {
// Code that might throw
} catch (e) {
if (e instanceof Error) {
// Handle error
}
}

This approach has two significant limitations:

  1. Cross-realm errors aren’t correctly identified. When an error originates from another realm (like an iframe or VM module), instanceof Error returns false because each realm has its own Error constructor.

This can lead to situations where errors are not handled correctly, as the instanceof check fails.

  1. Fake errors can pass the test. Objects with Error.prototype in their prototype chain but lacking error characteristics will be incorrectly identified as errors.

These issues can lead to inconsistent error handling and difficult-to-diagnose bugs. The new Error.isError() method provides a solution:

Instead of checking the prototype chain, Error.isError() uses a simpler and more reliable approach. It looks for a special internal marker (like a hidden ID tag) that gets added to every genuine Error object when it’s created.

This method works better than instanceof for two reasons:

  1. It correctly identifies errors even when they come from different contexts (like iframes or modules)
  2. It rejects fake objects that try to pretend they’re errors by manipulating the prototype

Think of it like checking for a manufacturer’s watermark instead of just looking at the label - it’s much harder to fake.

Typing Error.isError()

Here’s one way you could type the Error.isError() method in TypeScript:


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

JavaScript
6 min read

The creator of JavaScript and Node.js are challenging Oracle's control over the JavaScript name

Nov 23, 2024
Read article
JavaScript
7 min read

AggregateError holds several errors in one, the way Promise.any() reports that every promise rejected. How to throw and read it, plus the new SuppressedError.

Sep 2, 2024
Read article
JavaScript
4 min read

The 'with' statement is the only feature ever deprecated in JavaScript

Aug 22, 2024
Read article
JavaScript
4 min read

A left shift multiplies a number by 2 per position, and a right shift divides it by 2. How JavaScript's bitwise shift operators work, with binary examples.

Nov 12, 2024
Read article
JavaScript
7 min read

A WeakRef points to an object without stopping the garbage collector from freeing it. When that helps, how deref() works, and cleanup with FinalizationRegistry.

Jan 7, 2025
Read article
JavaScript
6 min read

Peaks.js is a client-side JavaScript component to display and interact with audio waveforms in the browser

Oct 22, 2019
Read article
JavaScript
5 min read

scheduler.postTask() runs JavaScript at user-blocking, user-visible or background priority, giving finer control than setTimeout(0) over when work runs.

Nov 26, 2024
Read article
JavaScript
6 min read

Closures are essential for creating functions that maintain state, without relying on global variables.

Sep 6, 2024
Read article
JavaScript
4 min read

Understanding the new import attributes syntax and why we can't rely on file extensions alone

Nov 10, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/error-iserror-javascript. It was written by a human and polished using grammar tools for clarity.