:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(toolbar): stop reporting handled preloaded-flags fetch failures as exceptions (#64972)
posthog[bot]71be28375bd3
diff --git a/frontend/src/toolbar/index.test.ts b/frontend/src/toolbar/index.test.tsindex 67e10da3818d..57b2c7fd7b63 100644--- a/frontend/src/toolbar/index.test.ts+++ b/frontend/src/toolbar/index.test.ts@@ -1,5 +1,12 @@import { ToolbarParams } from '~/types'+// Keep the real toolbar posthog-js instance (rendering depends on it) but spy on+// the exception capture helper so we can assert what does and doesn't get reported.+jest.mock('~/toolbar/toolbarPosthogJS', () => ({+ ...jest.requireActual('~/toolbar/toolbarPosthogJS'),+ captureToolbarException: jest.fn(),+}))+// Mock window and fetchconst mockFetch = jest.fn()@@ -121,6 +128,54 @@ describe('Toolbar flag loading', () => {consoleErrorSpy.mockRestore()})+ it.each([+ {+ name: 'transient network failure (fetch rejects)',+ // `fetch` rejects only on network-level failures — these should be logged, not captured.+ setupMock: () => mockFetch.mockRejectedValueOnce(new TypeError('Failed to fetch')),+ expectCapture: false,+ },+ {+ name: 'unexpected error while applying flags (TypeError thrown in processing)',+ // A null body makes `data.featureFlags` throw a TypeError during processing —+ // a genuine bug that must still reach error tracking.+ setupMock: () => mockFetch.mockResolvedValueOnce({ json: async () => null }),+ expectCapture: true,+ },+ ])('reports only genuine errors as exceptions: $name', async ({ setupMock, expectCapture }) => {+ const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation()+ const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation()++ await import('./index')+ const { captureToolbarException } = await import('~/toolbar/toolbarPosthogJS')++ const mockPostHog = {+ featureFlags: {+ overrideFeatureFlags: jest.fn(),+ },+ }++ const toolbarParams: ToolbarParams = {+ apiURL: 'http://localhost:8010',+ toolbarFlagsKey: 'test-key-123',+ token: 'test-token',+ }++ setupMock()++ await (window as any).ph_load_toolbar(toolbarParams, mockPostHog)++ if (expectCapture) {+ expect(captureToolbarException).toHaveBeenCalledWith(expect.anything(), 'preloaded_flags_fetch')+ } else {+ expect(captureToolbarException).not.toHaveBeenCalled()+ expect(consoleWarnSpy).toHaveBeenCalled()+ }++ consoleErrorSpy.mockRestore()+ consoleWarnSpy.mockRestore()+ })+it('should not fetch flags when toolbarFlagsKey is not present', async () => {await import('./index')diff --git a/frontend/src/toolbar/index.tsx b/frontend/src/toolbar/index.tsxindex 91b352623e82..a67210712340 100644--- a/frontend/src/toolbar/index.tsx+++ b/frontend/src/toolbar/index.tsx@@ -96,11 +96,26 @@ win['ph_load_toolbar'] = async function (toolbarParams: ToolbarParams, posthog?:canonicalizeApiHost(posthog.config?.api_host) ||canonicalizeApiHost(toolbarParams.apiURL) ||window.location.origin- await fetch(`${trimmedHost}/api/user/get_toolbar_preloaded_flags?key=${toolbarParams.toolbarFlagsKey}`, {- credentials: 'include',- })- .then((response) => response.json())- .then((data) => {+ const flagsUrl = `${trimmedHost}/api/user/get_toolbar_preloaded_flags?key=${toolbarParams.toolbarFlagsKey}`++ // Only the network call is wrapped here: `fetch` rejects solely on transient+ // network-level failures (aborted request, ad blocker / browser extension,+ // connectivity loss, CORS). The flags preload is best-effort and the toolbar+ // degrades cleanly without it, so these are logged rather than surfaced as+ // error-tracking exceptions. Parsing/applying the response is handled separately+ // below so genuine bugs there still get captured.+ let response: Response | undefined+ try {+ response = await fetch(flagsUrl, { credentials: 'include' })+ } catch (error) {+ toolbarLogger.warn('flags', 'Error fetching toolbar feature flags', {+ error: error instanceof Error ? error.message : String(error),+ })+ }++ if (response) {+ try {+ const data = await response.json()if (data.featureFlags) {posthog.featureFlags.overrideFeatureFlags({ flags: data.featureFlags })} else {@@ -110,11 +125,11 @@ win['ph_load_toolbar'] = async function (toolbarParams: ToolbarParams, posthog?:'preloaded_flags')}- })- .catch((error) => {- toolbarLogger.error('flags', 'Error fetching toolbar feature flags')+ } catch (error) {+ toolbarLogger.error('flags', 'Error processing toolbar feature flags')captureToolbarException(error, 'preloaded_flags_fetch')- })+ }+ }}initKeaInToolbar()