Skip to content

fix(analytics): only autocapture exceptions in production builds#797

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/gate-exception-autocapture-prod
Draft

fix(analytics): only autocapture exceptions in production builds#797
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/gate-exception-autocapture-prod

Conversation

@posthog

@posthog posthog Bot commented Jul 3, 2026

Copy link
Copy Markdown

Problem

A new error-tracking issue fired for a caught SyntaxError: Expected property name or '}' in JSON originating from the wizard's own unit tests — not from any real user. The stack traces through vitest into file-watcher.test.ts, whose 'swallows invalid JSON without throwing' case deliberately writes {not valid to a watched file. The watcher's try/catch correctly swallows it (mechanism.handled: true), yet it still lands in error tracking.

The reason: the module-level posthog-node client in src/utils/analytics.ts set enableExceptionAutocapture: true unconditionally, which installs a process-wide exception handler. Instantiated inside the test/dev process, it scoops up unrelated throws and reports them. Zero real-user impact — just noise that can bury real issues.

Changes

Gate enableExceptionAutocapture on IS_PRODUCTION_BUILD so only published builds autocapture exceptions. Dev/CI runs still send their normally-tagged (build: 'dev'/'ci') analytics — they just no longer hijack the process's exception handler. The file-watcher.ts try/catch is left as-is; it was already correct and is intentionally tested.

Test plan

  • pnpm build && pnpm test && pnpm fix — all 1156 unit tests pass, build and lint clean.
  • No test asserted on the old behavior; the change is a one-line flag gate.

Created with PostHog Code from an inbox report.

The module-level posthog-node client set `enableExceptionAutocapture: true`
unconditionally. That installs a process-wide exception handler that scoops up
any exception in the process — including ones from dev/test runs. A vitest suite
that deliberately exercises a caught JSON.parse ended up surfacing as a spurious
`$exception` issue in our own error tracking (handled: true, random per-process
anonymous IDs, $lib: posthog-node).

Gate the flag on IS_PRODUCTION_BUILD so dev/CI processes no longer hijack the
exception handler. Dev/CI runs still send their normally-tagged analytics; they
just stop autocapturing unrelated throws.

Generated-By: PostHog Code
Task-Id: c233e4a5-da14-495e-8fa8-48fb77f3f077
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci error-tracking-upload-source-maps
  • /wizard-ci mcp-analytics
  • /wizard-ci misc
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci error-tracking-upload-source-maps/android
  • /wizard-ci error-tracking-upload-source-maps/cicd-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-nested-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-single-stage-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-gitlab-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-monorepo-pnpm-node-react
  • /wizard-ci error-tracking-upload-source-maps/cicd-monorepo-raw-node-react
  • /wizard-ci error-tracking-upload-source-maps/cicd-ssh-vps-node-raw
  • /wizard-ci error-tracking-upload-source-maps/flutter
  • /wizard-ci error-tracking-upload-source-maps/ios
  • /wizard-ci error-tracking-upload-source-maps/next
  • /wizard-ci error-tracking-upload-source-maps/next-no-posthog
  • /wizard-ci error-tracking-upload-source-maps/node-raw
  • /wizard-ci error-tracking-upload-source-maps/node-rollup
  • /wizard-ci error-tracking-upload-source-maps/node-rollup-typescript-plugin
  • /wizard-ci error-tracking-upload-source-maps/node-webpack
  • /wizard-ci error-tracking-upload-source-maps/nuxt-3-6
  • /wizard-ci error-tracking-upload-source-maps/nuxt-4-3
  • /wizard-ci error-tracking-upload-source-maps/react-native
  • /wizard-ci error-tracking-upload-source-maps/react-vite
  • /wizard-ci error-tracking-upload-source-maps/rust
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci misc/quack-quack
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants