diff --git a/surfsense_web/app/error.tsx b/surfsense_web/app/error.tsx index 7bbd74e0e..3935f84d5 100644 --- a/surfsense_web/app/error.tsx +++ b/surfsense_web/app/error.tsx @@ -1,6 +1,6 @@ "use client"; -import posthog from "posthog-js"; + import { useEffect } from "react"; export default function ErrorPage({ @@ -11,7 +11,11 @@ export default function ErrorPage({ reset: () => void; }) { useEffect(() => { - posthog.captureException(error); + import("posthog-js") + .then(({ default: posthog }) => { + posthog.captureException(error); + }) + .catch(() => {}); }, [error]); return ( diff --git a/surfsense_web/lib/apis/base-api.service.ts b/surfsense_web/lib/apis/base-api.service.ts index 4c3371233..bc9e6c1d8 100644 --- a/surfsense_web/lib/apis/base-api.service.ts +++ b/surfsense_web/lib/apis/base-api.service.ts @@ -1,4 +1,3 @@ -import posthog from "posthog-js"; import type { ZodType } from "zod"; import { getBearerToken, handleUnauthorized, refreshAccessToken } from "../auth-utils"; import { AppError, AuthenticationError, AuthorizationError, NotFoundError } from "../error"; @@ -234,18 +233,21 @@ class BaseApiService { } catch (error) { console.error("Request failed:", JSON.stringify(error)); if (!(error instanceof AuthenticationError)) { - try { - posthog.captureException(error, { - api_url: url, - api_method: options?.method ?? "GET", - ...(error instanceof AppError && { - status_code: error.status, - status_text: error.statusText, - }), + import("posthog-js") + .then(({ default: posthog }) => { + posthog.captureException(error, { + api_url: url, + api_method: options?.method ?? "GET", + ...(error instanceof AppError && { + status_code: error.status, + status_text: error.statusText, + }), + }); + }) + .catch(() => { + // PostHog is not available in the current environment + console.error("Failed to capture exception in PostHog"); }); - } catch { - // PostHog capture failed — don't block the error flow - } } throw error; }