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): stop reporting Next's handled RSC fetch fallback to Sentry (#14890)
Bently57bf0c02373c
diff --git a/autogpt_platform/frontend/instrumentation-client.ts b/autogpt_platform/frontend/instrumentation-client.tsindex 6edec8dea7..80da7f5d92 100644--- a/autogpt_platform/frontend/instrumentation-client.ts+++ b/autogpt_platform/frontend/instrumentation-client.ts@@ -2,6 +2,7 @@// The config you add here will be used whenever a users loads a page in their browser.// https://docs.sentry.io/platforms/javascript/guides/nextjs/+import { isNextRSCNavigationFallback } from "@/lib/sentry-filters";import { setupSessionReplay } from "@/lib/session-replay";import { environment } from "@/services/environment";import * as Sentry from "@sentry/nextjs";@@ -24,6 +25,12 @@ Sentry.init({/Error invoking postEvent: Method not found/,],+ // Next's handled RSC fetch fallback reaches us only through console capture+ // and falls back to a full navigation, so it is noise (BUILDER-3QB).+ beforeSend(event) {+ return isNextRSCNavigationFallback(event) ? null : event;+ },+// Add optional integrations for additional featuresintegrations: [Sentry.captureConsoleIntegration({ levels: ["fatal", "error", "warn"] }),diff --git a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.tsindex 405dd03ac9..721728ebd9 100644--- a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts+++ b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts@@ -40,4 +40,29 @@ describe("Sentry client instrumentation", () => {expect(types).not.toContain("deprecation");expect(types).toEqual(expect.arrayContaining(["crash", "intervention"]));});++ it("drops Next's handled RSC fetch fallback but keeps other fetch errors", async () => {+ await import("../../instrumentation-client");++ const { beforeSend } = initMock.mock.calls[0][0];+ const fetchError = {+ exception: {+ values: [{ type: "TypeError", value: "Failed to fetch" }],+ },+ };+ const rscFallback = {+ ...fetchError,+ logger: "console",+ extra: {+ arguments: [+ "Failed to fetch RSC payload for https://platform.agpt.co/login. Falling back to browser navigation.",+ { name: "TypeError", message: "Failed to fetch" },+ ],+ },+ };++ expect(beforeSend).toBeTypeOf("function");+ expect(beforeSend(rscFallback, {})).toBeNull();+ expect(beforeSend(fetchError, {})).toBe(fetchError);+ });});diff --git a/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.ts b/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.tsnew file mode 100644index 0000000000..f0a78a4c26--- /dev/null+++ b/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.ts@@ -0,0 +1,114 @@+import * as Sentry from "@sentry/nextjs";+import {+ afterAll,+ afterEach,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from "vitest";++import {+ logClientRequestFailure,+ resetClientRequestFailureLog,+} from "@/app/api/mutators/request-failure-log";++import { isNextRSCNavigationFallback } from "../sentry-filters";++// Drives a real Sentry client with the same console capture as+// instrumentation-client.ts, so the events are built exactly as in production.+const sent: Sentry.ErrorEvent[] = [];++beforeAll(() => {+ // Spy before init so the console patch wraps the spy.+ vi.spyOn(console, "error").mockImplementation(() => undefined);+ vi.spyOn(console, "warn").mockImplementation(() => undefined);+ Sentry.init({+ dsn: "https://[email protected]/0",+ // No network: a failed send is itself logged, and would be captured too.+ transport: () => ({+ send: () => Promise.resolve({}),+ flush: () => Promise.resolve(true),+ }),+ integrations: [+ Sentry.captureConsoleIntegration({ levels: ["fatal", "error", "warn"] }),+ ],+ beforeSend(event) {+ if (!isNextRSCNavigationFallback(event)) sent.push(event);+ return null;+ },+ });+});++beforeEach(() => {+ sent.length = 0;+});++afterEach(() => {+ resetClientRequestFailureLog();+});++afterAll(() => {+ vi.restoreAllMocks();+});++describe("Sentry filter for Next's RSC navigation fallback", () => {+ it("drops the handled 'Failed to fetch RSC payload' console error", async () => {+ // Verbatim from next/dist/client/components/router-reducer/fetch-server-response.+ console.error(+ "Failed to fetch RSC payload for https://platform.agpt.co/login. Falling back to browser navigation.",+ new TypeError("Failed to fetch"),+ );+ await Sentry.flush(2000);++ expect(sent).toHaveLength(0);+ });++ it("still reports a fetch failure captured from our own code", async () => {+ Sentry.captureException(new TypeError("Failed to fetch"));+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ expect(sent[0].exception?.values?.[0]).toMatchObject({+ type: "TypeError",+ value: "Failed to fetch",+ });+ });++ it("still reports our own console error about a failed fetch", async () => {+ console.error("Failed to fetch agents:", new TypeError("Failed to fetch"));+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ expect(sent[0].extra?.arguments).toEqual([+ "Failed to fetch agents:",+ expect.anything(),+ ]);+ });++ it("still reports an API request failure logged by the mutator", async () => {+ logClientRequestFailure({+ status: 500,+ method: "GET",+ url: "/api/library/agents",+ errorMessage: "Internal Server Error",+ responseData: null,+ });+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ expect(sent[0].message).toContain("Request failed on client");+ });++ it("still reports a message that only borrows the RSC wording", async () => {+ console.error(+ "Failed to fetch RSC payload for https://platform.agpt.co/login",+ new TypeError("Failed to fetch"),+ );+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ });+});diff --git a/autogpt_platform/frontend/src/lib/sentry-filters.ts b/autogpt_platform/frontend/src/lib/sentry-filters.tsnew file mode 100644index 0000000000..b20f082921--- /dev/null+++ b/autogpt_platform/frontend/src/lib/sentry-filters.ts@@ -0,0 +1,22 @@+import type { Event } from "@sentry/nextjs";++// Next.js logs exactly this when a client-side navigation cannot fetch the RSC+// payload (flaky network, tab backgrounded, deploy in progress). It then does a+// full browser navigation to the same URL, so the user sees nothing wrong.+// See next/dist/client/components/router-reducer/fetch-server-response.+const NEXT_RSC_FALLBACK_MESSAGE =+ /^Failed to fetch RSC payload for .+\. Falling back to browser navigation\.$/;++/**+ * True for the Sentry event that captureConsoleIntegration builds from Next's+ * handled RSC fetch fallback. The event's own title is the underlying+ * "TypeError: Failed to fetch", so `ignoreErrors` cannot tell it apart from a+ * real fetch failure in our code; the console text in `extra.arguments` can.+ */+export function isNextRSCNavigationFallback(event: Event) {+ if (event.logger !== "console") return false;+ const args = event.extra?.arguments;+ if (!Array.isArray(args)) return false;+ const [message] = args;+ return typeof message === "string" && NEXT_RSC_FALLBACK_MESSAGE.test(message);+}