# IrtError

> The IrtError class of the Client API is the error a connection, a call or a room-wide request ends with.

The **`IrtError`** class of the [Client API](/docs/client) 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()`](/docs/client/connect), [`RoomClient`](/docs/client/room-client)'s methods and calls, and [`createRoom()`](/docs/client/createroom), [`joinByCode()`](/docs/client/joinbycode), [`listPublic()`](/docs/client/listpublic) and [`quickJoin()`](/docs/client/quickjoin) raise it. The [`close` event](/docs/client/room-client/close-event) carries one as its `error`. Player data, collection and leaderboard calls reject with [`DataError`](/docs/room/data-error) instead.

## Constructor

```ts
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`](/docs/client/irt-error/code).

`message`
: A string: the error's [`message`](/docs/client/irt-error/message).

`reason` Optional
: A string: the error's [`reason`](/docs/client/irt-error/reason). Defaults to `""`.

`name` Optional
: A string: the error's [`name`](/docs/client/irt-error/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`](/docs/client/irt-error/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`](/docs/client/irt-error/name)
: One of the web's error names for what happened, such as `"TimeoutError"` or `"VersionError"`.

[`IrtError.code`](/docs/client/irt-error/code)
: The detail: a close code, an HTTP status, 422 for a call the room failed, 1000 after `close()` or 0.

[`IrtError.reason`](/docs/client/irt-error/reason)
: The router's or the room's reason, such as a kick's, or `""` for none.

[`IrtError.message`](/docs/client/irt-error/message)
: A description of the error for people to read.

[`IrtError.cause`](/docs/client/irt-error/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](/docs/client/errors) 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.

```ts
// 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

- [Errors and close codes](/docs/client/errors)
- [`close` event](/docs/client/room-client/close-event)
- [`RoomCloseEvent`](/docs/client/room-close-event)
