Skip to content

Function: isError()

API / @xmachines/core / utils / isError

function isError(value): value is Error;

Defined in: packages/core/src/utils/is-error.ts:29

Tells if a value is an Error, by its identity or by its brand.

The function tries instanceof first. It therefore answers true for every object that inherits from Error.prototype of this realm, also one that no Error constructor built, such as Object.create(Error.prototype).

instanceof misses an error from another realm, such as an iframe or node:vm. For such a value the function uses Error.isError where the runtime has it (Node 24 and a Baseline 2025 browser), because that function refuses a false Symbol.toStringTag. In every other runtime the function tests the brand. The type of Error.isError is structural here, because the lib target of a consumer can be older than the API.

The function can throw for a hostile value: a revoked Proxy traps instanceof, and it traps the inspection of the brand too. A caller in an error path therefore wraps the call in try.

Parameters

ParameterTypeDescription
valueunknownThe value to test.

Returns

value is Error

true when the value is an Error of any realm.

Example

import { isError } from "@xmachines/core/utils";
isError(new TypeError("x")); // true
isError({ message: "x" }); // false