Skip to content

Commit fc5b7d6

Browse files
AbhiPrasadonurtemizkan
authored andcommitted
ref(hub): Remove optional chaining (#4292)
1 parent 9074f4e commit fc5b7d6

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

packages/hub/src/hub.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,18 @@ export class Hub implements HubInterface {
415415
* @inheritDoc
416416
*/
417417
public endSession(): void {
418-
this.getStackTop()
419-
?.scope?.getSession()
420-
?.close();
418+
const layer = this.getStackTop();
419+
const scope = layer && layer.scope;
420+
const session = scope && scope.getSession();
421+
if (session) {
422+
session.close();
423+
}
421424
this._sendSessionUpdate();
422425

423426
// the session is over; take it off of the scope
424-
this.getStackTop()?.scope?.setSession();
427+
if (scope) {
428+
scope.setSession();
429+
}
425430
}
426431

427432
/**
@@ -575,7 +580,8 @@ export function getActiveDomain(): DomainAsCarrier | undefined {
575580
*/
576581
function getHubFromActiveDomain(registry: Carrier): Hub {
577582
try {
578-
const activeDomain = getMainCarrier().__SENTRY__?.extensions?.domain?.active;
583+
const sentry = getMainCarrier().__SENTRY__;
584+
const activeDomain = sentry && sentry.extensions && sentry.extensions.domain && sentry.extensions.domain.active;
579585

580586
// If there's no active domain, just return global hub
581587
if (!activeDomain) {

packages/hub/src/scope.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ export class Scope implements ScopeInterface {
264264
const span = this.getSpan() as undefined | (Span & { spanRecorder: { spans: Span[] } });
265265

266266
// try it the new way first
267-
if (span?.transaction) {
268-
return span?.transaction;
267+
if (span && span.transaction) {
268+
return span.transaction;
269269
}
270270

271271
// fallback to the old way (known bug: this only finds transactions with sampled = true)
272-
if (span?.spanRecorder?.spans[0]) {
272+
if (span && span.spanRecorder && span.spanRecorder.spans[0]) {
273273
return span.spanRecorder.spans[0] as Transaction;
274274
}
275275

@@ -430,7 +430,7 @@ export class Scope implements ScopeInterface {
430430
// errors with transaction and it relies on that.
431431
if (this._span) {
432432
event.contexts = { trace: this._span.getTraceContext(), ...event.contexts };
433-
const transactionName = this._span.transaction?.name;
433+
const transactionName = this._span.transaction && this._span.transaction.name;
434434
if (transactionName) {
435435
event.tags = { transaction: transactionName, ...event.tags };
436436
}

packages/hub/src/sessionflusher.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ export class SessionFlusher implements SessionFlusherLike {
8383
return;
8484
}
8585
const scope = getCurrentHub().getScope();
86-
const requestSession = scope?.getRequestSession();
86+
const requestSession = scope && scope.getRequestSession();
8787

8888
if (requestSession && requestSession.status) {
8989
this._incrementSessionStatusCount(requestSession.status, new Date());
9090
// This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in
9191
// case captureRequestSession is called more than once to prevent double count
92-
scope?.setRequestSession(undefined);
93-
92+
if (scope) {
93+
scope.setRequestSession(undefined);
94+
}
9495
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
9596
}
9697
}

0 commit comments

Comments
 (0)