Skip to content

feat: add captureException() for error tracking#106

Open
hnipps wants to merge 1 commit intoPostHog:masterfrom
hnipps:feat/capture-exception
Open

feat: add captureException() for error tracking#106
hnipps wants to merge 1 commit intoPostHog:masterfrom
hnipps:feat/capture-exception

Conversation

@hnipps
Copy link

@hnipps hnipps commented Feb 27, 2026

Summary

Adds captureException() to the PHP SDK, enabling PHP applications to send exceptions to PostHog's error tracking — matching the existing Python and Node SDK implementations.

  • New ExceptionUtils class — converts \Throwable to $exception_list payload with stack frames, source context, and chained exception support
  • Client::captureException() — captures exceptions as $exception events with configurable source context lines
  • PostHog::captureException() — static facade for the same functionality
  • Anonymous capture — when no distinctId is provided, generates a UUID v4 and sets $process_person_profile: false

Key design decisions

  • No fingerprint generation — PostHog backend handles grouping (matches Python/Node)
  • No autocapture (set_exception_handler) — keeping initial PR scoped to manual capture; autocapture can follow in a separate PR
  • No args in frames — PHP trace includes function args but they can contain sensitive data and blow batch size limits
  • platform: "php" — matches Python's "python" and Node's "node:javascript" convention
  • Source context default 5 lines — matches Python SDK; configurable via exception_source_context_lines option (0 to disable)
  • Outermost-first ordering — chained exceptions are listed with outermost first, root cause last

Usage

// With distinct ID
PostHog::captureException($exception, 'user-123');

// Anonymous (generates UUID, disables person processing)
PostHog::captureException($exception);

// With additional properties
PostHog::captureException($exception, 'user-123', [
    'environment' => 'production',
    'release' => 'v1.2.3',
]);

// Unhandled exception
PostHog::captureException($exception, 'user-123', [], false);

// Disable source context
PostHog::init('your-api-key', [
    'exception_source_context_lines' => 0,
]);

Test plan

  • 20 new tests covering: basic capture, chained exceptions, anonymous IDs, handled/unhandled, frame ordering, in_app detection, source context on/off, PHP \Error support, static facade, additional properties, 3-deep chains
  • All 175 existing tests pass (zero regressions)

Closes PostHog/posthog#31076

🤖 Generated with Claude Code

Add exception capture support to the PHP SDK, matching the
functionality available in Python and Node SDKs. This enables
PHP applications to send exceptions to PostHog's error tracking.

- New ExceptionUtils class for converting Throwables to $exception_list
- Client::captureException() with chained exception support
- PostHog::captureException() static facade
- Source context capture (configurable, default 5 lines)
- Anonymous capture with UUID v4 when no distinctId provided
- Comprehensive test suite (20 tests, 140 assertions)

Closes PostHog/posthog#31076

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Feature request: error tracking support for PHP SDK

1 participant