From c7712200bb7a6478c083ee8f880b2b134841cb24 Mon Sep 17 00:00:00 2001 From: Pratyay360 Date: Sat, 28 Nov 2026 17:33:08 +0530 Subject: [PATCH] lcms --- src/hooks.client.ts | 6 ++- src/hooks.server.test.ts | 87 ++++++++++++++++++++++++++++++++++++++++ src/hooks.server.ts | 14 ++++--- 3 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/hooks.server.test.ts 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 new file mode 100644 index 0000000..73562e4 --- /dev/null +++ b/src/hooks.server.test.ts @@ -0,0 +1,87 @@ +import type { RequestEvent } from "@sveltejs/kit"; +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +const { captureExceptionMock } = vi.hoisted(() => ({ + captureExceptionMock: vi.fn(), +})); + +vi.mock("#lib/server/posthog", () => ({ + posthog: { + captureException: captureExceptionMock, + getContext: vi.fn(), + withContext: vi.fn(), + }, +})); + +vi.mock("posthog-node/otel", () => ({ + enrichCurrentSpan: vi.fn(), +})); + +import { handleError } from "./hooks.server"; + +function createMockEvent(posthogError?: RequestEvent["locals"]["posthogError"]): RequestEvent { + return { + locals: { posthogError }, + } as RequestEvent; +} + +describe("hooks.server handleError", () => { + beforeEach(() => { + captureExceptionMock.mockReset(); + }); + + 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("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("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("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 fd4b8bb..5a20a68 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,12 +59,15 @@ export async function handle({ event, resolve }) { }, { fresh: true }, ); -} +}; + +export const handleError: HandleServerError = ({ kind, error, event }) => { + if (kind !== "unknown") { + return; + } -export function handleError({ error, event }) { posthog.captureException(error, { distinctId: event.locals.posthogError?.distinctId, properties: event.locals.posthogError?.properties, }); - return { message: error instanceof Error ? error.message : "Internal Error" }; -} +};