Skip to content

Commit 8b462de

Browse files
committed
[v7] Inline prepareEvent into processEvent and run client event processors
1 parent 7a94f13 commit 8b462de

File tree

3 files changed

+45
-52
lines changed

3 files changed

+45
-52
lines changed

packages/core/src/baseclient.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import {
1212
dateTimestampInSeconds,
1313
getEventDescription,
14+
isPlainObject,
1415
isPrimitive,
1516
logger,
1617
normalize,
@@ -248,44 +249,6 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
248249
this._sendRequest(sessionToTransportRequest(session));
249250
}
250251

251-
/**
252-
* Adds common information to events.
253-
*
254-
* The information includes release and environment from `options`,
255-
* breadcrumbs and context (extra, tags and user) from the scope.
256-
*
257-
* Information that is already present in the event is never overwritten. For
258-
* nested objects, such as the context, keys are merged.
259-
*
260-
* @param event The original event.
261-
* @param hint May contain additional information about the original exception.
262-
* @returns A new event with more information.
263-
*/
264-
protected _prepareEvent(event: SentryEvent, captureContext: CaptureContext): SentryEvent | null {
265-
const { normalizeDepth = 3 } = this.options;
266-
const prepared: SentryEvent = {
267-
...event,
268-
event_id: event.event_id ?? uuid4(),
269-
timestamp: event.timestamp ?? dateTimestampInSeconds(),
270-
};
271-
272-
this._applyClientOptions(prepared);
273-
this._applyIntegrationsMetadata(prepared);
274-
275-
const scope =
276-
captureContext.scope instanceof Scope
277-
? captureContext.scope
278-
: this.getScope()
279-
.clone()
280-
.update(captureContext.scope);
281-
282-
const processedEvent = scope.applyToEvent(prepared, captureContext.hint);
283-
if (typeof normalizeDepth === 'number' && normalizeDepth > 0) {
284-
return this._normalizeEvent(processedEvent, normalizeDepth);
285-
}
286-
return processedEvent;
287-
}
288-
289252
/**
290253
* Applies `normalize` function on necessary `Event` attributes to make them safe for serialization.
291254
* Normalized keys:
@@ -434,6 +397,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
434397
* @param scope A scope containing event metadata.
435398
* @returns A Promise that resolves with the event or rejects in case event was/will not be send.
436399
*/
400+
// eslint-disable-next-line complexity
437401
protected _processEvent(event: SentryEvent, captureContext: CaptureContext): SentryEvent | null {
438402
if (this.options.enabled === false) {
439403
logger.error('SDK not enabled, will not send event.');
@@ -452,24 +416,57 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
452416
}
453417

454418
try {
455-
let processedEvent = this._prepareEvent(event, captureContext);
419+
let processedEvent: SentryEvent | null = {
420+
...event,
421+
event_id: event.event_id ?? uuid4(),
422+
timestamp: event.timestamp ?? dateTimestampInSeconds(),
423+
};
424+
425+
this._applyClientOptions(processedEvent);
426+
this._applyIntegrationsMetadata(processedEvent);
427+
428+
const scope =
429+
captureContext.scope instanceof Scope
430+
? captureContext.scope
431+
: this.getScope()
432+
.clone()
433+
.update(captureContext.scope);
434+
435+
processedEvent = scope.applyToEvent(processedEvent, captureContext.hint);
436+
if (processedEvent === null) {
437+
logger.error('A scope event processor returned null, will not send event.');
438+
return null;
439+
}
440+
441+
for (const processor of this._eventProcessors) {
442+
if (typeof processor === 'function') {
443+
const nextEvent = processor(processedEvent, captureContext.hint);
444+
if (nextEvent === null) {
445+
logger.error('A client event processor returned null, will not send event.');
446+
return null;
447+
}
448+
processedEvent = nextEvent;
449+
}
450+
}
456451

457452
if (processedEvent === null) {
458-
logger.error('An event processor returned null, will not send event.');
453+
logger.error('A scope event processor returned null, will not send event.');
459454
return null;
460455
}
461456

462-
const isInternalException =
463-
captureContext?.hint &&
464-
captureContext?.hint?.data &&
465-
(captureContext?.hint?.data as { __sentry__: boolean }).__sentry__ === true;
457+
const normalizeDepth = this.options.normalizeDepth ?? 3;
458+
if (typeof normalizeDepth === 'number' && normalizeDepth > 0) {
459+
processedEvent = this._normalizeEvent(processedEvent, normalizeDepth);
460+
}
461+
462+
const isInternalException = captureContext?.hint?.data?.__sentry__ === true;
466463
if (isInternalException || isTransaction || !this.options.beforeSend) {
467464
return processedEvent;
468465
}
469466

470-
processedEvent = this.options.beforeSend(processedEvent, captureContext?.hint);
467+
processedEvent = this.options.beforeSend(processedEvent as SentryEvent, captureContext?.hint);
471468

472-
if (typeof processedEvent === 'undefined') {
469+
if (!(isPlainObject(processedEvent) || processedEvent === null)) {
473470
logger.error('`beforeSend` method has to return `null` or a valid event.');
474471
return null;
475472
}

packages/integration-browser-linkederrors/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { computeStackTrace, exceptionFromStacktrace } from '@sentry/eventbuilder
55
const DEFAULT_KEY = 'cause';
66
const DEFAULT_LIMIT = 5;
77

8-
// TODO: Make this (and node version) built-in (?)
98
export class LinkedErrors implements Integration {
109
public name = this.constructor.name;
1110

packages/integration-node-linkederrors/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ export class LinkedErrors implements Integration {
2121
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
2222
return event;
2323
}
24-
25-
const linkedErrors = this._walkErrorTree(hint.originalException as Error, this._key);
26-
if (event && event.exception && event.exception.values) {
27-
event.exception.values = [...linkedErrors, ...event.exception.values];
28-
}
24+
const linkedErrors = this._walkErrorTree(hint.originalException as ExtendedError, this._key);
25+
event.exception.values = [...linkedErrors, ...event.exception.values];
2926
return event;
3027
});
3128
}

0 commit comments

Comments
 (0)