Skip to content

ref(core): Remove optional chaining #4291

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 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
*/
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
// ensure we haven't captured this very object before
if (hint?.originalException && checkOrSetAlreadyCaught(hint.originalException)) {
if (hint && hint.originalException && checkOrSetAlreadyCaught(hint.originalException)) {
logger.log(ALREADY_SEEN_ERROR);
return;
}
Expand Down Expand Up @@ -523,6 +523,15 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
const { beforeSend, sampleRate } = this.getOptions();
const transport = this.getTransport();

type RecordLostEvent = NonNullable<Transport['recordLostEvent']>;
type RecordLostEventParams = Parameters<RecordLostEvent>;

function recordLostEvent(outcome: RecordLostEventParams[0], category: RecordLostEventParams[1]): void {
if (transport.recordLostEvent) {
transport.recordLostEvent(outcome, category);
}
}

if (!this._isEnabled()) {
return SyncPromise.reject(new SentryError('SDK not enabled, will not capture event.'));
}
Expand All @@ -532,7 +541,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
// 0.0 === 0% events are sent
// Sampling for transaction happens somewhere else
if (!isTransaction && typeof sampleRate === 'number' && Math.random() > sampleRate) {
transport.recordLostEvent?.(Outcome.SampleRate, 'event');
recordLostEvent(Outcome.SampleRate, 'event');
return SyncPromise.reject(
new SentryError(
`Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`,
Expand All @@ -543,7 +552,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
return this._prepareEvent(event, scope, hint)
.then(prepared => {
if (prepared === null) {
transport.recordLostEvent?.(Outcome.EventProcessor, event.type || 'event');
recordLostEvent(Outcome.EventProcessor, event.type || 'event');
throw new SentryError('An event processor returned null, will not send event.');
}

Expand All @@ -557,7 +566,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
})
.then(processedEvent => {
if (processedEvent === null) {
transport.recordLostEvent?.(Outcome.BeforeSend, event.type || 'event');
recordLostEvent(Outcome.BeforeSend, event.type || 'event');
throw new SentryError('`beforeSend` returned `null`, will not send event.');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class InboundFilters implements Integration {
for (let i = frames.length - 1; i >= 0; i--) {
const frame = frames[i];

if (frame?.filename !== '<anonymous>' && frame?.filename !== '[native code]') {
if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') {
return frame.filename || null;
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export function initAndBind<F extends Client, O extends Options>(clientClass: Cl
logger.enable();
}
const hub = getCurrentHub();
hub.getScope()?.update(options.initialScope);
const scope = hub.getScope();
if (scope) {
scope.update(options.initialScope);
}
const client = new clientClass(options);
hub.bindClient(client);
}