Skip to content

Context not being reliably set in withScope. Potential v7 regression. #5261

Closed
@sacummings91

Description

@sacummings91

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/react

SDK Version

7.1.1

Framework Version

7.1.1

Link to Sentry event

https://sentry.io/organizations/ternary/issues/3191835938/?environment=test&project=5634786&query=is%3Aunresolved

Steps to Reproduce

I have a small class that wraps sentry for capturing messages and exceptions. As shown below, for some reason, context does not get reliably sent.

public reportMessage(
    message: string,
    options?: { context?: Context; tags?: Tags }
  ) {
    Sentry.withScope((scope) => {
      if (options?.context) {
        scope.setContext("context", this._stringifyContext(options.context))
      }

      if (options?.tags) {
        scope.setTags(options.tags);
      }

      Sentry.captureMessage(message);
    });
  }

Below are two examples where it works every time. And I do not understand why.

If I do anything extra like this console log:

Sentry.withScope((scope) => {
      if (options?.context) {
        console.log("literally anything")
        scope.setContext("context", this._stringifyContext(options.context));
      }

      if (options?.tags) {
        scope.setTags(options.tags);
      }

      Sentry.captureMessage(message);
    });

Or even just setting context to a new variable first like this:

Sentry.withScope((scope) => {
     if (options?.context) {
       const context = this._stringifyContext(options.context);
       scope.setContext("context", context);
     }

     if (options?.tags) {
       scope.setTags(options.tags);
     }

     Sentry.captureMessage(message);
   });

Is this a bug with 7.0? This didn't happen before I upgraded.

Expected Result

Custom context should show up on issue in Sentry.io console

Actual Result

Custom context does not show up in Sentry.io console

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions