Skip to content
Merged

a #37

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/hooks.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
93 changes: 65 additions & 28 deletions src/hooks.server.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
10 changes: 8 additions & 2 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
Loading