Skip to content

ref(hub): Remove optional chaining #4292

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
16 changes: 11 additions & 5 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 };
}
Expand Down
7 changes: 4 additions & 3 deletions packages/hub/src/sessionflusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}
Expand Down