From f84548d1e1f0534a4037e72694e0c7f3fb0e0c12 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 14 Dec 2021 20:33:50 -0500 Subject: [PATCH] ref(hub): Remove optional chaining --- packages/hub/src/hub.ts | 16 +++++++++++----- packages/hub/src/scope.ts | 8 ++++---- packages/hub/src/sessionflusher.ts | 7 ++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/hub/src/hub.ts b/packages/hub/src/hub.ts index 2c1f6b297faf..0ff7ef13ec7b 100644 --- a/packages/hub/src/hub.ts +++ b/packages/hub/src/hub.ts @@ -415,13 +415,18 @@ export class Hub implements HubInterface { * @inheritDoc */ public endSession(): void { - this.getStackTop() - ?.scope?.getSession() - ?.close(); + const layer = this.getStackTop(); + const scope = layer && layer.scope; + const session = scope && scope.getSession(); + if (session) { + session.close(); + } this._sendSessionUpdate(); // the session is over; take it off of the scope - this.getStackTop()?.scope?.setSession(); + if (scope) { + scope.setSession(); + } } /** @@ -575,7 +580,8 @@ export function getActiveDomain(): DomainAsCarrier | undefined { */ function getHubFromActiveDomain(registry: Carrier): Hub { try { - const activeDomain = getMainCarrier().__SENTRY__?.extensions?.domain?.active; + const sentry = getMainCarrier().__SENTRY__; + const activeDomain = sentry && sentry.extensions && sentry.extensions.domain && sentry.extensions.domain.active; // If there's no active domain, just return global hub if (!activeDomain) { diff --git a/packages/hub/src/scope.ts b/packages/hub/src/scope.ts index 196cbf09b5b7..21c17f4a214b 100644 --- a/packages/hub/src/scope.ts +++ b/packages/hub/src/scope.ts @@ -264,12 +264,12 @@ export class Scope implements ScopeInterface { const span = this.getSpan() as undefined | (Span & { spanRecorder: { spans: Span[] } }); // try it the new way first - if (span?.transaction) { - return span?.transaction; + if (span && span.transaction) { + return span.transaction; } // fallback to the old way (known bug: this only finds transactions with sampled = true) - if (span?.spanRecorder?.spans[0]) { + if (span && span.spanRecorder && span.spanRecorder.spans[0]) { return span.spanRecorder.spans[0] as Transaction; } @@ -430,7 +430,7 @@ export class Scope implements ScopeInterface { // errors with transaction and it relies on that. if (this._span) { event.contexts = { trace: this._span.getTraceContext(), ...event.contexts }; - const transactionName = this._span.transaction?.name; + const transactionName = this._span.transaction && this._span.transaction.name; if (transactionName) { event.tags = { transaction: transactionName, ...event.tags }; } diff --git a/packages/hub/src/sessionflusher.ts b/packages/hub/src/sessionflusher.ts index f3571f24ec0a..bb898030efb4 100644 --- a/packages/hub/src/sessionflusher.ts +++ b/packages/hub/src/sessionflusher.ts @@ -83,14 +83,15 @@ export class SessionFlusher implements SessionFlusherLike { return; } const scope = getCurrentHub().getScope(); - const requestSession = scope?.getRequestSession(); + const requestSession = scope && scope.getRequestSession(); if (requestSession && requestSession.status) { this._incrementSessionStatusCount(requestSession.status, new Date()); // This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in // case captureRequestSession is called more than once to prevent double count - scope?.setRequestSession(undefined); - + if (scope) { + scope.setRequestSession(undefined); + } /* eslint-enable @typescript-eslint/no-unsafe-member-access */ } }