From 0c9aaac934bb0915a9930b246cde8c663f67a9af Mon Sep 17 00:00:00 2001 From: "posthog[bot]" <206114724+posthog[bot]@users.noreply.github.com> Date: Sat, 28 Nov 2026 11:22:01 +0000 Subject: [PATCH] fix(hooks): stop reporting 404 errors to PostHog error tracking SvelteKit 3 sends unmatched routes to handleError as a framework error with status 404. The hook reported each one as an exception. Kit 3 does not pass a status argument in production, so the guard reads error.status for all kinds except unknown. Generated-By: PostHog Desktop Task-Id: bfe15e03-56da-4779-964f-6a8e014d11f5 --- src/hooks.server.test.ts | 50 ++++++++++++++++++++++++++++++++++++++++ src/hooks.server.ts | 13 +++++++---- 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/hooks.server.test.ts diff --git a/src/hooks.server.test.ts b/src/hooks.server.test.ts new file mode 100644 index 0000000..5b14f98 --- /dev/null +++ b/src/hooks.server.test.ts @@ -0,0 +1,50 @@ +import type { RequestEvent } from "@sveltejs/kit"; +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +const { captureException } = vi.hoisted(() => ({ captureException: vi.fn() })); + +vi.mock("#lib/server/posthog", () => ({ + posthog: { captureException }, +})); + +const { handleError } = await import("./hooks.server"); + +const event = { + locals: { + posthogError: { distinctId: "user-1", properties: { $session_id: "session-1" } }, + }, +} as unknown as RequestEvent; + +describe("handleError", () => { + beforeEach(() => { + captureException.mockReset(); + }); + + it("does not report a framework 404", () => { + handleError({ kind: "framework", error: { status: 404, message: "Not Found" }, event }); + expect(captureException).not.toHaveBeenCalled(); + }); + + it("does not report an app 404", () => { + handleError({ kind: "app", error: { status: 404, message: "Not Found" }, event }); + expect(captureException).not.toHaveBeenCalled(); + }); + + it("reports a framework error that is not a 404", () => { + const error = { status: 405, message: "Method Not Allowed" }; + handleError({ kind: "framework", error, event }); + expect(captureException).toHaveBeenCalledWith(error, { + distinctId: "user-1", + properties: { $session_id: "session-1" }, + }); + }); + + it("reports an unknown error", () => { + const error = new Error("boom"); + expect(handleError({ kind: "unknown", error, event })).toEqual({ message: "boom" }); + expect(captureException).toHaveBeenCalledWith(error, { + distinctId: "user-1", + properties: { $session_id: "session-1" }, + }); + }); +}); diff --git a/src/hooks.server.ts b/src/hooks.server.ts index fd4b8bb..2d94d34 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -60,10 +60,13 @@ export async function handle({ event, resolve }) { ); } -export function handleError({ error, event }) { - posthog.captureException(error, { - distinctId: event.locals.posthogError?.distinctId, - properties: event.locals.posthogError?.properties, - }); +export function handleError({ error, event, kind }) { + const notFound = kind !== "unknown" && error.status === 404; + if (!notFound) { + posthog.captureException(error, { + distinctId: event.locals.posthogError?.distinctId, + properties: event.locals.posthogError?.properties, + }); + } return { message: error instanceof Error ? error.message : "Internal Error" }; }