0

Looking at this example from the Sentry docs - how does the Sentry.captureException know that it's being called within Sentry.withScope?

The Sentry.captureException method will need to access the parameters assigned to that scope such as the user. But no reference of the scope is passed to the function.

    Sentry.withScope(function(scope) {
        scope.setUser(someUser);
        Sentry.captureException(error);
    });

The only way I can imagine to pass data between the two functions is via some kind of try catch trickery. I don't see another way that the Sentry.captureException can know about the parent scope and the parameters associated with that scope.

Adam Carter
  • 145
  • 1
  • 7
  • [What is the 'Execution Context' in JavaScript exactly?](https://stackoverflow.com/questions/9384758/), [How does the "this" keyword work, and when should it be used?](https://stackoverflow.com/questions/3127429/) – Andreas Dec 27 '22 at 12:40
  • Maybe a bit more up-to-date: https://stackoverflow.com/a/73355400/1169519 and http://dmitrysoshnikov.com/ecmascript/javascript-the-core-2nd-edition/#environment – Teemu Dec 27 '22 at 12:45
  • @Teemu That's a different "scope" than the Sentry term – Bergi Dec 27 '22 at 16:39

1 Answers1

0

Have a look at how the Sentry object is implemented. For example, it could be achieved with this:

class ScopedSentry {
  withScope(f) {
    f({setUser: function(user) {
      this.user = user;
    }.bind(this)});
  }
  captureException(error) {
    console.error(error, this.user);
  }
}
var Sentry = new ScopedSentry();
Sentry.withScope(function(scope) {
  scope.setUser("someUser");
  Sentry.captureException("error");
});
Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
  • This is good but it creates a single global scope rather than a local scope. So after `setUser` is called, all `captureException` will return the user even if they're not called within the `withScope` – Adam Carter Dec 27 '22 at 13:04
  • 1
    No, it creates a scope that is bound to the individual `ScopedSentry`. If you have another `Sentry2 = new ScopedSentry()`, each will have its own scope. – Heiko Theißen Dec 27 '22 at 13:06
  • Yes, you're right. But the actual library uses one actual static `Sentry` object which I think poses the real challenge. There's no value in `withScope` if you have to use a specific `ScopedSentry` – Adam Carter Dec 27 '22 at 13:11
  • I think this one needs a real javascript expert to solve – Adam Carter Dec 27 '22 at 13:27
  • @AdamCarter Only you need to become an expert is to read [this post](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) and the posts and articles I and Andreas have linked in the comments above. – Teemu Dec 27 '22 at 14:20
  • @AdamCarter Just do `try { f(…); } finally { this.user = undefined; }` if you don't want to keep the value after the `withScope` call – Bergi Dec 27 '22 at 16:38
  • What if `f` is asynchronous? – Heiko Theißen Dec 27 '22 at 17:16