earendil-works/pi · error · PiDisconnectedError

Pi client is disconnected

Error message

Pi client is disconnected

What it means

Thrown by PiClient.send() when the client's lifecycle state is not 'connected', meaning the underlying byte transport was already closed or failed. The generic sentinel message wraps the real disconnect cause reported by the transport handlers.

Solutions

  1. Reconnect the client (retry the connection/handshake) before sending frames
  2. Check the lifecycle state before calling send() and treat non-connected states as a reconnect signal
  3. Inspect the underlying transport error that triggered the disconnect via error listeners instead of relying on send() failures
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at packages/client/src/connection.ts:103 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/85c4ce08ae69714c. Report an issue: GitHub.

Appendix: source

Thrown at packages/client/src/connection.ts:103

				if (this.#isCurrent(id)) this.#failAndClose(toDisconnectedError(error));
			},
		} satisfies ByteTransportHandlers;
		void this.#openTransport(id, handlers);
		return handshake.promise;
	}

	disconnect(reason: string | Error = "Client disconnected"): void {
		if (this.#lifecycle.state === "disconnected") return;
		this.#failAndClose(typeof reason === "string" ? new PiDisconnectedError(reason) : reason);
	}

	fail(error: Error): void {
		this.#failAndClose(error);
	}

	send(frame: Uint8Array): void {
		const lifecycle = this.#lifecycle;
		if (lifecycle.state !== "connected") throw new PiDisconnectedError();
		let sending: Promise<void>;
		try {
			sending = lifecycle.transport.send(frame);
		} catch (error) {
			this.#failAndClose(toDisconnectedError(error));
			return;
		}
		void sending.catch((error: unknown) => {
			const current = this.#lifecycle;
			if (current.state !== "disconnected" && current.transport === lifecycle.transport) {
				this.#failAndClose(toDisconnectedError(error));
			}
		});
	}

	async #openTransport(id: number, handlers: ByteTransportHandlers): Promise<void> {
		let transport: ByteTransport;
		try {

View on GitHub (pinned to 4af9d21d3b)