diff --git a/src/hooks.client.ts b/src/hooks.client.ts index 321bc99..f82c606 100644 --- a/src/hooks.client.ts +++ b/src/hooks.client.ts @@ -43,7 +43,11 @@ export function init() { }); } -export const handleError: HandleClientError = ({ error }) => { +export const handleError: HandleClientError = ({ kind, error }) => { + if (kind !== "unknown") { + return; + } + if (!posthogInitialized) { return; } diff --git a/src/hooks.server.test.ts b/src/hooks.server.test.ts index 5b14f98..73562e4 100644 --- a/src/hooks.server.test.ts +++ b/src/hooks.server.test.ts @@ -1,50 +1,87 @@ import type { RequestEvent } from "@sveltejs/kit"; import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; -const { captureException } = vi.hoisted(() => ({ captureException: vi.fn() })); +const { captureExceptionMock } = vi.hoisted(() => ({ + captureExceptionMock: vi.fn(), +})); vi.mock("#lib/server/posthog", () => ({ - posthog: { captureException }, + posthog: { + captureException: captureExceptionMock, + getContext: vi.fn(), + withContext: vi.fn(), + }, +})); + +vi.mock("posthog-node/otel", () => ({ + enrichCurrentSpan: vi.fn(), })); -const { handleError } = await import("./hooks.server"); +import { handleError } from "./hooks.server"; -const event = { - locals: { - posthogError: { distinctId: "user-1", properties: { $session_id: "session-1" } }, - }, -} as unknown as RequestEvent; +function createMockEvent(posthogError?: RequestEvent["locals"]["posthogError"]): RequestEvent { + return { + locals: { posthogError }, + } as RequestEvent; +} -describe("handleError", () => { +describe("hooks.server handleError", () => { beforeEach(() => { - captureException.mockReset(); + captureExceptionMock.mockReset(); }); - it("does not report a framework 404", () => { - handleError({ kind: "framework", error: { status: 404, message: "Not Found" }, event }); - expect(captureException).not.toHaveBeenCalled(); + it("ignores framework errors such as 404 Not Found", async () => { + const result = await handleError({ + kind: "framework", + error: { status: 404, message: "Not Found" }, + event: createMockEvent(), + }); + + expect(captureExceptionMock).not.toHaveBeenCalled(); + expect(result).toBeUndefined(); }); - it("does not report an app 404", () => { - handleError({ kind: "app", error: { status: 404, message: "Not Found" }, event }); - expect(captureException).not.toHaveBeenCalled(); + it("ignores expected app errors thrown via error()", async () => { + const result = await handleError({ + kind: "app", + error: { message: "Document not found" }, + event: createMockEvent(), + }); + + expect(captureExceptionMock).not.toHaveBeenCalled(); + expect(result).toBeUndefined(); }); - 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("ignores validation errors", async () => { + const result = await handleError({ + kind: "validation", + error: { status: 400, message: "Bad Request" }, + issues: [], + event: createMockEvent(), }); + + expect(captureExceptionMock).not.toHaveBeenCalled(); + expect(result).toBeUndefined(); }); - 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" }, + it("captures unknown errors in PostHog with request metadata", async () => { + const unexpected = new Error("Database connection failed"); + const posthogError = { + distinctId: "user-123", + properties: { + $session_id: "session-456", + $trace_id: "trace-789", + }, + }; + + const result = await handleError({ + kind: "unknown", + error: unexpected, + event: createMockEvent(posthogError), }); + + expect(captureExceptionMock).toHaveBeenCalledTimes(1); + expect(captureExceptionMock).toHaveBeenCalledWith(unexpected, posthogError); + expect(result).toBeUndefined(); }); }); diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 2d94d34..5f753bf 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,4 +1,5 @@ import { context, isSpanContextValid, trace } from "@opentelemetry/api"; +import type { Handle, HandleServerError } from "@sveltejs/kit/hooks"; import { enrichCurrentSpan } from "posthog-node/otel"; import { posthog } from "#lib/server/posthog"; @@ -27,7 +28,7 @@ function tracingId(value: string | null | undefined): string | undefined { ); } -export async function handle({ event, resolve }) { +export const handle: Handle = async ({ event, resolve }) => { const existing = posthog.getContext(); const data = { distinctId: @@ -58,7 +59,12 @@ export async function handle({ event, resolve }) { }, { fresh: true }, ); -} +}; + +export const handleError: HandleServerError = ({ kind, error, event }) => { + if (kind !== "unknown") { + return; + } export function handleError({ error, event, kind }) { const notFound = kind !== "unknown" && error.status === 404;