mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-08 20:25:19 +02:00
feat: enhance error handling with PostHog integration
- Added PostHog error capturing for non-authentication errors in BaseApiService. - Refactored PostHog client initialization to ensure a single instance is used across the application.
This commit is contained in:
parent
9f17d834f6
commit
1ab395a34e
5 changed files with 122 additions and 8 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import posthog from "posthog-js";
|
||||
import type { ZodType } from "zod";
|
||||
import { getBearerToken, handleUnauthorized, refreshAccessToken } from "../auth-utils";
|
||||
import { AppError, AuthenticationError, AuthorizationError, NotFoundError } from "../error";
|
||||
|
|
@ -231,6 +232,20 @@ class BaseApiService {
|
|||
return data;
|
||||
} 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,
|
||||
}),
|
||||
});
|
||||
} catch {
|
||||
// PostHog capture failed — don't block the error flow
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,19 @@
|
|||
import { PostHog } from "posthog-node";
|
||||
|
||||
let posthogInstance: PostHog | null = null;
|
||||
|
||||
export default function PostHogClient() {
|
||||
if (!process.env.NEXT_PUBLIC_POSTHOG_KEY) {
|
||||
throw new Error("NEXT_PUBLIC_POSTHOG_KEY is not set");
|
||||
}
|
||||
|
||||
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
||||
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
||||
// Because server-side functions in Next.js can be short-lived,
|
||||
// we set flushAt to 1 and flushInterval to 0 to ensure events are sent immediately
|
||||
flushAt: 1,
|
||||
flushInterval: 0,
|
||||
});
|
||||
if (!posthogInstance) {
|
||||
posthogInstance = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
||||
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
||||
flushAt: 1,
|
||||
flushInterval: 0,
|
||||
});
|
||||
}
|
||||
|
||||
return posthogClient;
|
||||
return posthogInstance;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue