Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): report WebSocket close code and reason instead of "[object CloseEvent]" (#14717)

Bently62b20c3607e9
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts
index ccbed9b2e8..c75b34adff 100644
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts
@@ -1,4 +1,9 @@
import { afterEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@/lib/auth/actions", () => ({
+ getWebSocketToken: vi.fn(async () => ({ token: "test-token" })),
+}));
+
import BackendAPI, { buildOAuthLoginQuery } from "../client";
describe("BackendAPI.oAuthLogin", () => {
@@ -118,3 +123,98 @@ describe("BackendAPI._makeClientRequest 204 handling", () => {
expect(result).toEqual({ ok: true });
});
});
+
+describe("BackendAPI WebSocket failure logging", () => {
+ const sockets: FakeWebSocket[] = [];
+
+ class FakeWebSocket {
+ static readonly CONNECTING = 0;
+ static readonly OPEN = 1;
+ static readonly CLOSING = 2;
+ static readonly CLOSED = 3;
+
+ readyState = FakeWebSocket.CONNECTING;
+ onopen: (() => void) | null = null;
+ onclose:
+ | ((event: Pick<CloseEvent, "code" | "reason" | "wasClean">) => void)
+ | null = null;
+ onerror:
+ | ((event: Pick<Event, "type"> & { target?: unknown }) => void)
+ | null = null;
+ onmessage: ((event: MessageEvent) => void) | null = null;
+ state = "connecting";
+ close = vi.fn();
+ send = vi.fn();
+
+ constructor(public url: string) {
+ sockets.push(this);
+ }
+ }
+
+ afterEach(() => {
+ sockets.length = 0;
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ });
+
+ async function connect() {
+ vi.useFakeTimers();
+ vi.stubGlobal("WebSocket", FakeWebSocket);
+ const api = new BackendAPI("http://test", "ws://test/ws");
+ void api.connectWebSocket();
+ await vi.waitFor(() => expect(sockets).toHaveLength(1), { interval: 1 });
+ return sockets[0];
+ }
+
+ it("reports the close code and reason instead of the CloseEvent object", async () => {
+ const consoleError = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => undefined);
+ const socket = await connect();
+
+ socket.onclose!({ code: 4002, reason: "Invalid token", wasClean: true });
+
+ expect(consoleError).toHaveBeenCalledTimes(1);
+ const [message, ...rest] = consoleError.mock.calls[0];
+ expect(rest).toHaveLength(0);
+ expect(message).toContain("[BackendAPI] WebSocket failed to connect");
+ expect(message).toContain("4002");
+ expect(message).toContain("Invalid token");
+ expect(message).not.toContain("[object");
+ });
+
+ it("reports a close on an established connection at warn level", async () => {
+ const consoleWarn = vi
+ .spyOn(console, "warn")
+ .mockImplementation(() => undefined);
+ const socket = await connect();
+ socket.state = "connected";
+
+ socket.onclose!({ code: 1006, reason: "", wasClean: false });
+
+ const [message] = consoleWarn.mock.calls[0];
+ expect(message).toContain("[BackendAPI] WebSocket connection closed");
+ expect(message).toContain("1006");
+ expect(message).toContain("abnormal closure");
+ expect(message).not.toContain("[object");
+ });
+
+ it("reports the socket state on an error event instead of the Event object", async () => {
+ const consoleError = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => undefined);
+ const socket = await connect();
+ socket.state = "connected";
+ socket.readyState = FakeWebSocket.CLOSED;
+
+ socket.onerror!({ type: "error", target: socket });
+
+ const [message, ...rest] = consoleError.mock.calls[0];
+ expect(rest).toHaveLength(0);
+ expect(message).toContain("[BackendAPI] WebSocket error");
+ expect(message).toContain("CLOSED");
+ expect(message).not.toContain("[object");
+ expect(message).not.toContain("test-token");
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/websocket-logging-sentry.test.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/websocket-logging-sentry.test.ts
new file mode 100644
index 0000000000..f3e5b42382
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/websocket-logging-sentry.test.ts
@@ -0,0 +1,61 @@
+import * as Sentry from "@sentry/nextjs";
+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
+
+import { describeCloseEvent, logWebSocketIssue } from "../websocket-logging";
+
+// Pins the actual mechanism the fix relies on: the Sentry event is created by
+// captureConsoleIntegration from our console call, so it has to pick up both
+// the summary (as its title) and the scope extras set around that call.
+const events: Sentry.ErrorEvent[] = [];
+
+beforeAll(() => {
+ // Spy before init so the console patch wraps the spy: Sentry's integration
+ // only sees calls made through the console it patched.
+ vi.spyOn(console, "error").mockImplementation(() => undefined);
+ Sentry.init({
+ dsn: "https://[email protected]/0",
+ integrations: [
+ Sentry.captureConsoleIntegration({ levels: ["error", "warn"] }),
+ ],
+ beforeSend(event) {
+ events.push(event);
+ return null;
+ },
+ });
+});
+
+afterAll(() => {
+ vi.restoreAllMocks();
+});
+
+describe("websocket logging reaching Sentry", () => {
+ it("sends the close code and reason as the event title, with extras", async () => {
+ const { summary, extra } = describeCloseEvent(
+ { code: 4002, reason: "Invalid token", wasClean: true },
+ "wss://ws.example.com/ws?token=secret-token",
+ "connecting",
+ );
+
+ logWebSocketIssue(
+ "error",
+ `[BackendAPI] WebSocket failed to connect: ${summary}`,
+ extra,
+ );
+ await Sentry.flush(2000);
+
+ expect(events).toHaveLength(1);
+ const event = events[0];
+ expect(event.message).toBe(
+ '[BackendAPI] WebSocket failed to connect: code 4002 (invalid token), reason "Invalid token", wasClean true',
+ );
+ expect(event.extra).toMatchObject({
+ ws_close_code: 4002,
+ ws_close_code_name: "invalid token",
+ ws_close_reason: "Invalid token",
+ ws_was_clean: true,
+ ws_phase: "connecting",
+ ws_url: "wss://ws.example.com/ws",
+ });
+ expect(JSON.stringify(event)).not.toContain("secret-token");
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/websocket-logging.test.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/websocket-logging.test.ts
new file mode 100644
index 0000000000..c5cde3691e
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/websocket-logging.test.ts
@@ -0,0 +1,178 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+const setExtras = vi.fn();
+vi.mock("@sentry/nextjs", () => ({
+ withScope: (callback: (scope: { setExtras: (e: unknown) => void }) => void) =>
+ callback({ setExtras }),
+}));
+
+import {
+ describeCloseEvent,
+ describeErrorEvent,
+ logWebSocketIssue,
+} from "../websocket-logging";
+
+afterEach(() => {
+ vi.restoreAllMocks();
+ setExtras.mockClear();
+});
+
+describe("describeCloseEvent", () => {
+ it("puts the code, its name, the reason and wasClean in the summary", () => {
+ const { summary } = describeCloseEvent(
+ { code: 4002, reason: "Invalid token", wasClean: true },
+ "wss://ws.example.com/ws",
+ "connecting",
+ );
+
+ expect(summary).toContain("4002");
+ expect(summary).toContain("invalid token");
+ expect(summary).toContain("Invalid token");
+ expect(summary).toContain("wasClean true");
+ });
+
+ it("names an abnormal close and marks an empty reason", () => {
+ // 1006 with an empty reason is the case that used to log as
+ // "WebSocket failed to connect: [object CloseEvent]".
+ const { summary, extra } = describeCloseEvent(
+ { code: 1006, reason: "", wasClean: false },
+ "wss://ws.example.com/ws",
+ "connecting",
+ );
+
+ expect(summary).toBe(
+ 'code 1006 (abnormal closure), reason "(none)", wasClean false',
+ );
+ expect(extra).toEqual({
+ ws_close_code: 1006,
+ ws_close_code_name: "abnormal closure",
+ ws_close_reason: "",
+ ws_was_clean: false,
+ ws_phase: "connecting",
+ ws_url: "wss://ws.example.com/ws",
+ });
+ });
+
+ it("labels a code it does not know instead of dropping it", () => {
+ const { summary, extra } = describeCloseEvent(
+ { code: 4999, reason: "", wasClean: false },
+ "wss://ws.example.com/ws",
+ "connected",
+ );
+
+ expect(summary).toContain("4999");
+ expect(summary).toContain("unknown code");
+ expect(extra.ws_phase).toBe("connected");
+ });
+
+ it("strips the query string so the auth token is never reported", () => {
+ const { extra } = describeCloseEvent(
+ { code: 1000, reason: "", wasClean: true },
+ "wss://ws.example.com/ws?token=secret-token",
+ "connected",
+ );
+
+ expect(extra.ws_url).toBe("wss://ws.example.com/ws");
+ expect(JSON.stringify(extra)).not.toContain("secret-token");
+ });
+
+ it("strips the token wherever it sits in the query, even from a non-URL", () => {
+ const { summary, extra } = describeCloseEvent(
+ { code: 1000, reason: "", wasClean: true },
+ "not-a-url?x=1&token=secret-token",
+ "connected",
+ );
+
+ expect(extra.ws_url).toBe("not-a-url");
+ expect(summary + JSON.stringify(extra)).not.toContain("secret-token");
+ });
+
+ it("treats a missing reason as empty", () => {
+ const { summary, extra } = describeCloseEvent(
+ {
+ code: 1000,
+ reason: undefined as unknown as string,
+ wasClean: true,
+ },
+ "wss://ws.example.com/ws",
+ "connected",
+ );
+
+ expect(summary).toContain('reason "(none)"');
+ expect(extra.ws_close_reason).toBe("");
+ });
+});
+
+describe("describeErrorEvent", () => {
+ it("reports the event type, the socket state and the target URL", () => {
+ const { summary, extra } = describeErrorEvent(
+ {
+ type: "error",
+ target: { readyState: 3, url: "wss://ws.example.com/ws?token=secret" },
+ },
+ "wss://fallback.example.com/ws",
+ );
+
+ expect(summary).toBe(
+ 'type "error", readyState CLOSED, url wss://ws.example.com/ws',
+ );
+ expect(extra).toEqual({
+ ws_event_type: "error",
+ ws_ready_state: 3,
+ ws_ready_state_name: "CLOSED",
+ ws_url: "wss://ws.example.com/ws",
+ });
+ expect(JSON.stringify(extra)).not.toContain("secret");
+ });
+
+ it("labels a readyState it does not recognise", () => {
+ const { summary, extra } = describeErrorEvent(
+ {
+ type: "error",
+ target: { readyState: 7, url: "wss://ws.example.com/ws" },
+ },
+ "wss://ws.example.com/ws",
+ );
+
+ expect(summary).toContain("readyState unknown");
+ expect(extra.ws_ready_state).toBe(7);
+ });
+
+ it("falls back to the configured URL when the event has no target", () => {
+ const { summary, extra } = describeErrorEvent(
+ { type: "", target: null },
+ "wss://ws.example.com/ws",
+ );
+
+ expect(summary).toBe(
+ 'type "error", readyState unknown, url wss://ws.example.com/ws',
+ );
+ expect(extra.ws_ready_state).toBeNull();
+ });
+});
+
+describe("logWebSocketIssue", () => {
+ it("logs the message alone and hands the details to Sentry as extras", () => {
+ const consoleError = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => undefined);
+
+ logWebSocketIssue("error", "[BackendAPI] boom", { ws_close_code: 4002 });
+
+ // Only the string: passing the event object here is what Sentry's console
+ // integration used to stringify into "[object CloseEvent]".
+ expect(consoleError).toHaveBeenCalledWith("[BackendAPI] boom");
+ expect(consoleError.mock.calls[0]).toHaveLength(1);
+ expect(setExtras).toHaveBeenCalledWith({ ws_close_code: 4002 });
+ });
+
+ it("logs at warn level when asked to", () => {
+ const consoleWarn = vi
+ .spyOn(console, "warn")
+ .mockImplementation(() => undefined);
+
+ logWebSocketIssue("warn", "[BackendAPI] closed", {});
+
+ expect(consoleWarn).toHaveBeenCalledWith("[BackendAPI] closed");
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts
index 0150d20e85..9732cbc2a6 100644
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts
@@ -55,6 +55,11 @@ import type {
UsersBalanceHistoryResponse,
WebSocketNotification,
} from "./types";
+import {
+ describeCloseEvent,
+ describeErrorEvent,
+ logWebSocketIssue,
+} from "./websocket-logging";
const isClient = environment.isClientSide();
@@ -1089,16 +1094,26 @@ export default class BackendAPI {
};
this.webSocket.onclose = (event) => {
- if (this.webSocket?.state == "connecting") {
- console.error(
- `[BackendAPI] WebSocket failed to connect: ${event.reason}`,
- event,
- );
- } else if (this.webSocket?.state == "connected") {
- console.warn(
- `[BackendAPI] WebSocket connection closed: ${event.reason}`,
+ const phase = this.webSocket?.state;
+ if (phase == "connecting" || phase == "connected") {
+ const { summary, extra } = describeCloseEvent(
event,
+ this.wsUrl,
+ phase,
);
+ if (phase == "connecting") {
+ logWebSocketIssue(
+ "error",
+ `[BackendAPI] WebSocket failed to connect: ${summary}`,
+ extra,
+ );
+ } else {
+ logWebSocketIssue(
+ "warn",
+ `[BackendAPI] WebSocket connection closed: ${summary}`,
+ extra,
+ );
+ }
}
this.webSocket!.state = "closed";
@@ -1119,7 +1134,12 @@ export default class BackendAPI {
this.webSocket.onerror = (error) => {
if (this.webSocket?.state == "connected") {
- console.error("[BackendAPI] WebSocket error:", error);
+ const { summary, extra } = describeErrorEvent(error, this.wsUrl);
+ logWebSocketIssue(
+ "error",
+ `[BackendAPI] WebSocket error: ${summary}`,
+ extra,
+ );
}
};
this.webSocket.onmessage = (event) => this._handleWSMessage(event);
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/websocket-logging.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/websocket-logging.ts
new file mode 100644
index 0000000000..67b3aa7b58
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/websocket-logging.ts
@@ -0,0 +1,115 @@
+import * as Sentry from "@sentry/nextjs";
+
+/**
+ * `captureConsoleIntegration` (see `instrumentation-client.ts`) turns every
+ * console.error/warn into a Sentry event whose title is the console arguments
+ * `String()`-ed and joined with a space, so any object argument becomes
+ * "[object …]". These helpers flatten WebSocket events into a readable summary,
+ * which is logged as the only console argument, plus structured Sentry extras.
+ */
+
+// RFC 6455 status codes, plus the 4xxx codes the backend sends itself
+// (see autogpt_platform/backend/backend/api/ws_api.py).
+const CLOSE_CODE_NAMES: Record<number, string> = {
+ 1000: "normal closure",
+ 1001: "going away",
+ 1002: "protocol error",
+ 1003: "unsupported data",
+ 1005: "no status received",
+ 1006: "abnormal closure",
+ 1007: "invalid frame payload data",
+ 1008: "policy violation",
+ 1009: "message too big",
+ 1010: "mandatory extension missing",
+ 1011: "internal server error",
+ 1012: "service restart",
+ 1013: "try again later",
+ 1014: "bad gateway",
+ 1015: "TLS handshake failure",
+ 4001: "missing authentication token",
+ 4002: "invalid token",
+ 4003: "invalid token",
+};
+
+const READY_STATE_NAMES: Record<number, string> = {
+ 0: "CONNECTING",
+ 1: "OPEN",
+ 2: "CLOSING",
+ 3: "CLOSED",
+};
+
+export type WebSocketEventDetails = {
+ /** Goes into the log line, and so into the Sentry issue title. */
+ summary: string;
+ /** Goes into the Sentry event's extras. */
+ extra: Record<string, unknown>;
+};
+
+export function describeCloseEvent(
+ event: Pick<CloseEvent, "code" | "reason" | "wasClean">,
+ url: string,
+ phase: "connecting" | "connected",
+): WebSocketEventDetails {
+ const name = CLOSE_CODE_NAMES[event.code] ?? "unknown code";
+ const reason = event.reason || "(none)";
+ return {
+ summary: `code ${event.code} (${name}), reason "${reason}", wasClean ${Boolean(
+ event.wasClean,
+ )}`,
+ extra: {
+ ws_close_code: event.code,
+ ws_close_code_name: name,
+ ws_close_reason: event.reason || "",
+ ws_was_clean: Boolean(event.wasClean),
+ ws_phase: phase,
+ ws_url: stripQuery(url),
+ },
+ };
+}
+
+export function describeErrorEvent(
+ event: Pick<Event, "type"> & { target?: unknown },
+ url: string,
+): WebSocketEventDetails {
+ // The spec deliberately gives error events no detail, so the socket's own
+ // state is all there is to report beyond the event type.
+ const target = event.target as { readyState?: number; url?: string } | null;
+ const readyState = target?.readyState;
+ const readyStateName =
+ readyState !== undefined
+ ? (READY_STATE_NAMES[readyState] ?? "unknown")
+ : "unknown";
+ const targetUrl = stripQuery(target?.url ?? url);
+ return {
+ summary: `type "${event.type || "error"}", readyState ${readyStateName}, url ${targetUrl}`,
+ extra: {
+ ws_event_type: event.type || "error",
+ ws_ready_state: readyState ?? null,
+ ws_ready_state_name: readyStateName,
+ ws_url: targetUrl,
+ },
+ };
+}
+
+/**
+ * Logs `message` and attaches `extra` to the Sentry event that
+ * `captureConsoleIntegration` creates from that same console call: the
+ * integration captures on a fork of the scope that is current while console
+ * runs, so extras set here are inherited by the event.
+ */
+export function logWebSocketIssue(
+ level: "error" | "warn",
+ message: string,
+ extra: Record<string, unknown>,
+): void {
+ Sentry.withScope((scope) => {
+ scope.setExtras(extra);
+ console[level](message);
+ });
+}
+
+/** Keeps the auth token out of anything we report. */
+function stripQuery(url: string): string {
+ const queryStart = url.indexOf("?");
+ return queryStart === -1 ? url : url.slice(0, queryStart);
+}