fix(node): captureException uses distinctId from request context#3282
Merged
marandaneto merged 3 commits intomainfrom Mar 25, 2026
Merged
fix(node): captureException uses distinctId from request context#3282marandaneto merged 3 commits intomainfrom
marandaneto merged 3 commits intomainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: packages/node/src/__tests__/context.spec.ts
Line: 463-503
Comment:
**Prefer parameterised tests**
The three tests in `captureException with context` have a very similar structure (set up context, call `captureException`, assert on `distinct_id` and `$process_person_profile`). Per the project's simplicity rules, consider collapsing them into a single `it.each` table to express the same three scenarios more concisely and make it easier to add new cases in the future:
```ts
it.each([
{
label: 'no distinctId and no context → personless',
contextDistinctId: undefined,
explicitDistinctId: undefined,
expectedDistinctId: (id: string) => expect(typeof id).toBe('string'),
expectedProcessPersonProfile: false,
},
{
label: 'context distinctId only → uses context',
contextDistinctId: 'context-user',
explicitDistinctId: undefined,
expectedDistinctId: (id: string) => expect(id).toBe('context-user'),
expectedProcessPersonProfile: undefined,
},
{
label: 'explicit distinctId overrides context',
contextDistinctId: 'context-user',
explicitDistinctId: 'explicit-user',
expectedDistinctId: (id: string) => expect(id).toBe('explicit-user'),
expectedProcessPersonProfile: undefined,
},
])('captureException $label', async ({ contextDistinctId, explicitDistinctId, expectedDistinctId, expectedProcessPersonProfile }) => {
const run = () => posthog.captureException(new Error('test error'), explicitDistinctId)
contextDistinctId ? posthog.withContext({ distinctId: contextDistinctId }, run) : run()
await waitForFlush()
const events = getLastBatchEvents()
expect(events).toHaveLength(1)
expect(events?.[0].event).toBe('$exception')
expectedDistinctId(events?.[0].distinct_id)
expect(events?.[0].properties.$process_person_profile).toBe(expectedProcessPersonProfile)
})
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(node): captureException uses distinc..." | Re-trigger Greptile |
ablaszkiewicz
approved these changes
Mar 25, 2026
Contributor
Contributor
|
Size Change: -196 B (0%) Total Size: 6.56 MB
ℹ️ View Unchanged
|
cat-ph
approved these changes
Mar 25, 2026
Member
Author
flakiness, i fixed! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #3280
When using the Node SDK with request context (e.g., Express middleware or NestJS),
captureExceptiondoes not pick up thedistinctIdfrom the request context. Instead, it generates a random UUID and sets$process_person_profile = false, even when adistinctIdis available viawithContext().The root cause is that
ErrorTracking.buildEventMessage()eagerly resolves thedistinctId— either using the explicitly passed value or falling back touuidv7(). By the time the event reachesprepareEventMessage()(which checks request context), thedistinctIdis already set, so the context lookup is never reached.Changes
packages/node/src/extensions/error-tracking/index.ts: Removed theuuidv7()fallback and$process_person_profile = falsefrombuildEventMessage(). Now passesdistinctIdthrough as-is (may beundefined), lettingprepareEventMessage()handle context resolution and anonymous fallback — which already has the correct logic for both.packages/node/src/__tests__/context.spec.ts: Added 3 parameterised test cases viait.eachcovering:captureExceptionpicks updistinctIdfrom context when none is provided explicitlydistinctIdtakes priority over contextdistinctIdand no contextRelease info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file