IrtError: cause property

Available since @irtio/client 5.0BrowsersNode

The cause read-only property of the IrtError interface is what the error wraps: the reason an aborted signal carried, or the error of a request that didn’t complete.

Value

Any value, or undefined when the error wraps nothing. It’s the signal’s reason for an AbortError or a TimeoutError that a signal caused, as fetch() rejects with it, and the failed fetch()’s error for a NetworkError from a room-wide request.

Description

cause is the standard Error property. Branch on name and code, and use cause to see what the game passed to AbortController.abort().

Examples

Telling a cancel button from a timeout

The join screen aborts with a reason when the player cancels, so a game can tell the two apart.

// cancel-reason.ts
import { connect, IrtError } from "@irtio/client";

const joining = new AbortController();
document.querySelector("#cancel")?.addEventListener("click", () => joining.abort("cancelled"));

try {
  await connect("lobby", { signal: joining.signal });
} catch (e) {
  if (e instanceof IrtError && e.name === "AbortError") console.log(`stopped: ${String(e.cause)}`);
  else throw e;
}

See also

Runtime availability

BrowsersNode
cause5.05.0