Skip to content

ref(core): Extract _ensureBeforeSendRv into func #4288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
}

const beforeSendResult = beforeSend(prepared, hint);
return this._ensureBeforeSendRv(beforeSendResult);
return _ensureBeforeSendRv(beforeSendResult);
})
.then(processedEvent => {
if (processedEvent === null) {
Expand Down Expand Up @@ -611,29 +611,27 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
},
);
}
}

/**
* Verifies that return value of configured `beforeSend` is of expected type.
*/
protected _ensureBeforeSendRv(
rv: PromiseLike<Event | null> | Event | null,
): PromiseLike<Event | null> | Event | null {
const nullErr = '`beforeSend` method has to return `null` or a valid event.';
if (isThenable(rv)) {
return (rv as PromiseLike<Event | null>).then(
event => {
if (!(isPlainObject(event) || event === null)) {
throw new SentryError(nullErr);
}
return event;
},
e => {
throw new SentryError(`beforeSend rejected with ${e}`);
},
);
} else if (!(isPlainObject(rv) || rv === null)) {
throw new SentryError(nullErr);
}
return rv;
/**
* Verifies that return value of configured `beforeSend` is of expected type.
*/
function _ensureBeforeSendRv(rv: PromiseLike<Event | null> | Event | null): PromiseLike<Event | null> | Event | null {
const nullErr = '`beforeSend` method has to return `null` or a valid event.';
if (isThenable(rv)) {
return rv.then(
event => {
if (!(isPlainObject(event) || event === null)) {
throw new SentryError(nullErr);
}
return event;
},
e => {
throw new SentryError(`beforeSend rejected with ${e}`);
},
);
} else if (!(isPlainObject(rv) || rv === null)) {
throw new SentryError(nullErr);
}
return rv;
}