Skip to content

Commit 090410c

Browse files
committed
refactor: simplify php error tracking internals
1 parent 7250486 commit 090410c

10 files changed

Lines changed: 1865 additions & 2022 deletions

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,21 @@ Opt-in automatic capture from the core SDK:
3737

3838
```php
3939
PostHog::init('phc_xxx', [
40-
'enable_error_tracking' => true,
41-
'capture_uncaught_exceptions' => true,
42-
'capture_errors' => true,
43-
'capture_fatal_errors' => true,
44-
'error_reporting_mask' => E_ALL,
45-
'excluded_exceptions' => [
46-
\InvalidArgumentException::class,
40+
'error_tracking' => [
41+
'enabled' => true,
42+
'capture_errors' => true,
43+
'excluded_exceptions' => [
44+
\InvalidArgumentException::class,
45+
],
46+
'context_provider' => static function (array $payload): array {
47+
return [
48+
'distinctId' => $_SESSION['user_id'] ?? null,
49+
'properties' => [
50+
'$current_url' => $_SERVER['REQUEST_URI'] ?? null,
51+
],
52+
];
53+
},
4754
],
48-
'error_tracking_include_source_context' => true,
49-
'error_tracking_context_lines' => 5,
50-
'error_tracking_max_frames' => 20,
51-
'error_tracking_context_provider' => static function (array $payload): array {
52-
return [
53-
'distinctId' => $_SESSION['user_id'] ?? null,
54-
'properties' => [
55-
'$current_url' => $_SERVER['REQUEST_URI'] ?? null,
56-
],
57-
];
58-
},
5955
]);
6056
```
6157

example.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,17 @@ function errorTrackingExamples()
525525
'host' => $_ENV['POSTHOG_HOST'] ?? 'https://app.posthog.com',
526526
'debug' => true,
527527
'ssl' => !str_starts_with($_ENV['POSTHOG_HOST'] ?? 'https://app.posthog.com', 'http://'),
528-
'enable_error_tracking' => true,
529-
'capture_uncaught_exceptions' => true,
530-
'capture_errors' => true,
531-
'capture_fatal_errors' => true,
532-
'error_reporting_mask' => E_ALL,
533-
'error_tracking_context_provider' => static function (array $payload): array {
534-
return [
535-
'distinctId' => 'sdk-demo-user',
536-
'properties' => [
537-
'$error_source' => $payload['source'],
538-
],
539-
];
540-
},
528+
'error_tracking' => [
529+
'enabled' => true,
530+
'context_provider' => static function (array $payload): array {
531+
return [
532+
'distinctId' => 'sdk-demo-user',
533+
'properties' => [
534+
'$error_source' => $payload['source'],
535+
],
536+
];
537+
},
538+
],
541539
],
542540
null,
543541
$_ENV['POSTHOG_PERSONAL_API_KEY']

lib/Client.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function __construct(
126126
$this->distinctIdsFeatureFlagsReported = new SizeLimitedHash(SIZE_LIMIT);
127127
$this->flagsEtag = null;
128128

129-
ErrorTrackingRegistrar::configure($this, $options);
129+
ExceptionCapture::configure($this, $options['error_tracking'] ?? []);
130130

131131
// Populate featureflags and grouptypemapping if possible
132132
if (
@@ -205,16 +205,19 @@ public function captureException(
205205
$distinctId = Uuid::v4();
206206
}
207207

208-
$exceptionList = $this->buildExceptionList($exception);
209-
if ($exceptionList === null) {
208+
$errorTrackingConfig = $this->options['error_tracking'] ?? [];
209+
$maxFrames = max(0, (int) ($errorTrackingConfig['max_frames'] ?? 20));
210+
211+
$exceptionList = ExceptionPayloadBuilder::buildExceptionList($exception, $maxFrames);
212+
if (empty($exceptionList)) {
210213
return false;
211214
}
212215

213216
$properties = array_merge(
214217
$additionalProperties,
215218
[
216219
'$exception_list' => $exceptionList,
217-
'$exception_handled' => ExceptionCapture::getPrimaryHandled($exceptionList),
220+
'$exception_handled' => ExceptionPayloadBuilder::getPrimaryHandled($exceptionList),
218221
]
219222
);
220223

@@ -921,22 +924,6 @@ public function flush()
921924
return true;
922925
}
923926

924-
/**
925-
* @param \Throwable|string $exception
926-
* @return array|null
927-
*/
928-
private function buildExceptionList(\Throwable|string $exception): ?array
929-
{
930-
ExceptionCapture::configure($this->options);
931-
932-
$exceptionList = ExceptionCapture::buildParsedException($exception);
933-
if ($exceptionList === null) {
934-
return null;
935-
}
936-
937-
return ExceptionCapture::normalizeExceptionList($exceptionList);
938-
}
939-
940927
/**
941928
* Formats a timestamp by making sure it is set
942929
* and converting it to iso8601.

0 commit comments

Comments
 (0)