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:
DESKTOP-RTLN3BA\$punk 2026-03-12 01:28:39 -07:00
parent 9f17d834f6
commit 1ab395a34e
5 changed files with 122 additions and 8 deletions

View file

@ -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;
}
}