Skip to content

Commit b56a573

Browse files
authored
ref(core): Pre-beforeSendTransaction cleanup (#6135)
This is a collection of a bunch of small and mostly dumb fixes pulled from #6121 (along with a few others I happened upon in the course of working on it), in order to make that PR easier to review. Some of the changes are cosmetic (formatting and wordsmithing test names, adding comments, etc.), some are refactors for clarity (mostly renaming of variables), one or two are test changes which will be necessary for the tests introduced in the aforementioned PR (in order to keep the `beforeSend` and `beforeSendTransaction` tests as parallel as possible, in case we ever want to parameterize them), one is pulling unneeded extra data out of a test to make it simpler, and two are actual fixes, namely: - In the outcomes test, we were recording a session being dropped by `beforeSend`, but sessions don't go through `beforeSend`, so it's now an error event. - In many places in the base client tests, we were using `.toBe()` rather than `toEqual()` for values (like numbers and strings) which as far as I know aren't guaranteed to be singletons (the way `true` and `false` are, for example). This switches to using `toEqual()` in those cases.
1 parent 24e2a27 commit b56a573

File tree

7 files changed

+105
-107
lines changed

7 files changed

+105
-107
lines changed

packages/core/src/baseclient.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
637637
.then(prepared => {
638638
if (prepared === null) {
639639
this.recordDroppedEvent('event_processor', event.type || 'error');
640-
throw new SentryError('An event processor returned null, will not send event.', 'log');
640+
throw new SentryError('An event processor returned `null`, will not send event.', 'log');
641641
}
642642

643643
const isInternalException = hint.data && (hint.data as { __sentry__: boolean }).__sentry__ === true;
@@ -646,7 +646,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
646646
}
647647

648648
const beforeSendResult = beforeSend(prepared, hint);
649-
return _ensureBeforeSendRv(beforeSendResult);
649+
return _validateBeforeSendResult(beforeSendResult);
650650
})
651651
.then(processedEvent => {
652652
if (processedEvent === null) {
@@ -764,24 +764,26 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
764764
}
765765

766766
/**
767-
* Verifies that return value of configured `beforeSend` is of expected type.
767+
* Verifies that return value of configured `beforeSend` is of expected type, and returns the value if so.
768768
*/
769-
function _ensureBeforeSendRv(rv: PromiseLike<Event | null> | Event | null): PromiseLike<Event | null> | Event | null {
770-
const nullErr = '`beforeSend` method has to return `null` or a valid event.';
771-
if (isThenable(rv)) {
772-
return rv.then(
769+
function _validateBeforeSendResult(
770+
beforeSendResult: PromiseLike<Event | null> | Event | null,
771+
): PromiseLike<Event | null> | Event | null {
772+
const invalidValueError = '`beforeSend` must return `null` or a valid event.';
773+
if (isThenable(beforeSendResult)) {
774+
return beforeSendResult.then(
773775
event => {
774-
if (!(isPlainObject(event) || event === null)) {
775-
throw new SentryError(nullErr);
776+
if (!isPlainObject(event) && event !== null) {
777+
throw new SentryError(invalidValueError);
776778
}
777779
return event;
778780
},
779781
e => {
780782
throw new SentryError(`beforeSend rejected with ${e}`);
781783
},
782784
);
783-
} else if (!(isPlainObject(rv) || rv === null)) {
784-
throw new SentryError(nullErr);
785+
} else if (!isPlainObject(beforeSendResult) && beforeSendResult !== null) {
786+
throw new SentryError(invalidValueError);
785787
}
786-
return rv;
788+
return beforeSendResult;
787789
}

0 commit comments

Comments
 (0)