IrtError

Available since @irtio/client 5.0BrowsersNode

The IrtError class of the Client API is the error the client SDK throws and rejects with when a connection, a call or a room-wide request fails. It extends Error.

connect(), RoomClient’s methods and calls, and createRoom(), joinByCode(), listPublic() and quickJoin() raise it. The close event carries one as its error. Player data, collection and leaderboard calls reject with DataError instead.

Constructor

new IrtError(code, message)
new IrtError(code, message, reason)
new IrtError(code, message, reason, name)
new IrtError(code, message, reason, name, options)
code
A number: the error’s code.
message
A string: the error’s message.
reason Optional
A string: the error’s reason. Defaults to "".
name Optional
A string: the error’s name, one of the names it lists. Defaults to the name for code: the close code’s or HTTP status’s name where it has one, "NetworkError" for 0 and "OperationError" for any other code.
options Optional
An object with a cause property: the error’s cause.

The client constructs the errors it raises. Construct one yourself in a test, to stand in for one of them.

Instance properties

All are read only.

IrtError.name
One of the web’s error names for what happened, such as "TimeoutError" or "VersionError".
IrtError.code
The detail: a close code, an HTTP status, 422 for a call the room failed, 1000 after close() or 0.
IrtError.reason
The router’s or the room’s reason, such as a kick’s, or "" for none.
IrtError.message
A description of the error for people to read.
IrtError.cause
What the error wraps, such as an aborted signal’s reason.

Description

An IrtError’s name is one of the error names fetch(), WebSocket and the DOM use, so a game branches on e.name as it would for a web API. Where one name covers several causes, code tells them apart, and the CloseCode export names the close codes: CloseCode.Superseded is 4012.

IrtError isn’t a DOMException, whose own numeric code means something else. Check an error with instanceof IrtError, then its name or code. instanceof holds for errors from any copy of the SDK on the page.

Errors and close codes lists each close code’s causes and the messages each function rejects with.

Examples

Telling the player why they couldn’t join

A join screen catches what connect() rejects with and shows a sentence for the errors a player can act on.

// join.ts
import { CloseCode, connect, IrtError, reloadForNewBuild } from "@irtio/client";

export async function join(roomUrl: string) {
  try {
    return await connect(roomUrl, { signal: AbortSignal.timeout(20_000) });
  } catch (e) {
    if (!(e instanceof IrtError)) throw e;
    if (e.name === "VersionError") reloadForNewBuild();
    else if (e.code === CloseCode.RoomFull) console.warn("That room is full.");
    else if (e.code === CloseCode.Superseded) console.warn("You're playing in another tab.");
    else if (e.name === "TimeoutError" || e.name === "NetworkError") console.warn("Couldn't reach the game. Try again?");
    else console.error(e.message);
    return undefined;
  }
}

See also

Runtime availability

BrowsersNode
IrtError5.05.0