Skip to content

fix: Make sure that getSession exists before calling it #3017

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
Oct 29, 2020
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
2 changes: 1 addition & 1 deletion packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
throw new SentryError('`beforeSend` returned `null`, will not send event.');
}

const session = scope && scope.getSession();
const session = scope && scope.getSession && scope.getSession();
Copy link

@MrChocolatine MrChocolatine Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about simply using ?.? (same comment for the second file in this PR)

const session = scope && scope.getSession?.();

Is its compatibility enough for you?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#Browser_compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could do that, as we're compiling it down to ES5 anyway. Good catch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For that matter, couldn't we just do

const session = scope?.getSession?.();

?

if (!isTransaction && session) {
this._updateSessionFromEvent(session, processedEvent);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export class Hub implements HubInterface {
const { scope, client } = this.getStackTop();
if (!scope) return;

const session = scope.getSession();
const session = scope.getSession && scope.getSession();
if (session) {
session.close();
if (client && client.captureSession) {
Expand Down