Skip to content

Commit e9d0c67

Browse files
committed
fix(core): ensure LinkedErrors integration runs before eventProcessors
1 parent 6643671 commit e9d0c67

File tree

4 files changed

+65
-59
lines changed

4 files changed

+65
-59
lines changed

packages/browser/src/integrations/linkederrors.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
1+
import type { EventProcessor, Hub, Integration } from '@sentry/types';
22
import { applyAggregateErrorsToEvent } from '@sentry/utils';
33

44
import { exceptionFromError } from '../eventbuilder';
@@ -45,28 +45,29 @@ export class LinkedErrors implements Integration {
4545
/**
4646
* @inheritDoc
4747
*/
48-
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
49-
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
50-
const hub = getCurrentHub();
51-
const client = hub.getClient();
52-
const self = hub.getIntegration(LinkedErrors);
48+
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
49+
const client = getCurrentHub().getClient();
50+
if (client && client.on) {
51+
client.on('preprocessEvent', (event, hint) => {
52+
const hub = getCurrentHub();
53+
const client = hub.getClient();
54+
const self = hub.getIntegration(LinkedErrors);
5355

54-
if (!client || !self) {
55-
return event;
56-
}
56+
if (!client || !self) {
57+
return;
58+
}
5759

58-
const options = client.getOptions();
59-
applyAggregateErrorsToEvent(
60-
exceptionFromError,
61-
options.stackParser,
62-
options.maxValueLength,
63-
self._key,
64-
self._limit,
65-
event,
66-
hint,
67-
);
68-
69-
return event;
70-
});
60+
const options = client.getOptions();
61+
applyAggregateErrorsToEvent(
62+
exceptionFromError,
63+
options.stackParser,
64+
options.maxValueLength,
65+
self._key,
66+
self._limit,
67+
event,
68+
hint,
69+
);
70+
});
71+
}
7172
}
7273
}

packages/core/src/baseclient.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
384384
public on(hook: 'beforeEnvelope', callback: (envelope: Envelope) => void): void;
385385

386386
/** @inheritdoc */
387-
public on(hook: 'beforeSendEvent', callback: (event: Event, hint?: EventHint) => void): void;
387+
public on(hook: 'beforeSendEvent' | 'preprocessEvent', callback: (event: Event, hint?: EventHint) => void): void;
388388

389389
/** @inheritdoc */
390390
public on(
@@ -418,7 +418,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
418418
public emit(hook: 'beforeEnvelope', envelope: Envelope): void;
419419

420420
/** @inheritdoc */
421-
public emit(hook: 'beforeSendEvent', event: Event, hint?: EventHint): void;
421+
public emit(hook: 'beforeSendEvent' | 'preprocessEvent', event: Event, hint?: EventHint): void;
422422

423423
/** @inheritdoc */
424424
public emit(hook: 'afterSendEvent', event: Event, sendResponse: TransportMakeRequestResponse | void): void;
@@ -528,6 +528,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
528528
if (!hint.integrations && integrations.length > 0) {
529529
hint.integrations = integrations;
530530
}
531+
532+
this.emit('preprocessEvent', event, hint);
533+
531534
return prepareEvent(options, event, hint, scope).then(evt => {
532535
if (evt === null) {
533536
return evt;

packages/node/src/integrations/linkederrors.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
1+
import type { EventProcessor, Hub, Integration } from '@sentry/types';
22
import { applyAggregateErrorsToEvent } from '@sentry/utils';
33

44
import { exceptionFromError } from '../eventbuilder';
5-
import { ContextLines } from './contextlines';
65

76
const DEFAULT_KEY = 'cause';
87
const DEFAULT_LIMIT = 5;
@@ -40,36 +39,31 @@ export class LinkedErrors implements Integration {
4039
/**
4140
* @inheritDoc
4241
*/
43-
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
44-
addGlobalEventProcessor(async (event: Event, hint?: EventHint) => {
45-
const hub = getCurrentHub();
46-
const client = hub.getClient();
47-
const self = hub.getIntegration(LinkedErrors);
42+
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
43+
const client = getCurrentHub().getClient();
44+
if (client && client.on) {
45+
client.on('preprocessEvent', (event, hint) => {
46+
const hub = getCurrentHub();
47+
const client = hub.getClient();
4848

49-
if (!client || !self) {
50-
return event;
51-
}
49+
const self = hub.getIntegration(LinkedErrors);
5250

53-
const options = client.getOptions();
51+
if (!client || !self) {
52+
return;
53+
}
5454

55-
applyAggregateErrorsToEvent(
56-
exceptionFromError,
57-
options.stackParser,
58-
options.maxValueLength,
59-
self._key,
60-
self._limit,
61-
event,
62-
hint,
63-
);
55+
const options = client.getOptions();
6456

65-
// If the ContextLines integration is enabled, we add source code context to linked errors
66-
// because we can't guarantee the order that integrations are run.
67-
const contextLines = getCurrentHub().getIntegration(ContextLines);
68-
if (contextLines) {
69-
await contextLines.addSourceContext(event);
70-
}
71-
72-
return event;
73-
});
57+
applyAggregateErrorsToEvent(
58+
exceptionFromError,
59+
options.stackParser,
60+
options.maxValueLength,
61+
self._key,
62+
self._limit,
63+
event,
64+
hint,
65+
);
66+
});
67+
}
7468
}
7569
}

packages/types/src/client.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,15 @@ export interface Client<O extends ClientOptions = ClientOptions> {
177177
on?(hook: 'beforeEnvelope', callback: (envelope: Envelope) => void): void;
178178

179179
/**
180-
* Register a callback for before an event is sent.
180+
* Register a callback for before sending an event.
181+
* `beforeSendEvent` is called right before an event is sent and should not be used to mutate the event.
182+
* `preprocessEvent` is called before all global event processors.
183+
* Receives an Event & EventHint as arguments.
181184
*/
182-
on?(hook: 'beforeSendEvent', callback: (event: Event, hint?: EventHint | void) => void): void;
185+
on?(
186+
hook: 'beforeSendEvent' | 'preprocessEvent',
187+
callback: (event: Event, hint?: EventHint | undefined) => void,
188+
): void;
183189

184190
/**
185191
* Register a callback for when an event has been sent.
@@ -217,11 +223,13 @@ export interface Client<O extends ClientOptions = ClientOptions> {
217223
*/
218224
emit?(hook: 'beforeEnvelope', envelope: Envelope): void;
219225

220-
/*
221-
* Fire a hook event before sending an event. Expects to be given an Event & EventHint as the
222-
* second/third argument.
226+
/**
227+
* Fire a hook event before sending an event.
228+
* `beforeSendEvent` is called right before an event is sent and should not be used to mutate the event.
229+
* `preprocessEvent` is called before all global event processors.
230+
* Expects to be given an Event & EventHint as the second/third argument.
223231
*/
224-
emit?(hook: 'beforeSendEvent', event: Event, hint?: EventHint): void;
232+
emit?(hook: 'beforeSendEvent' | 'preprocessEvent', event: Event, hint?: EventHint): void;
225233

226234
/*
227235
* Fire a hook event after sending an event. Expects to be given an Event as the

0 commit comments

Comments
 (0)