question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling multiple user contexts

See original GitHub issue

Hi,

I was wondering how to add a user context to something like

Async.each(users, (user, callback) => {

	Sentry.configureScope((scope) => {

		scope.setUser(user);
    });

	// Do something async for the user
	// capture error for user in/after that async function
	// Resulting user context will be the last user
}, callback);

I’ve tried creating a specific hub but that had the same issue

const userSpecificHub = new Sentry.Hub(Sentry.getCurrentHub().getClient());
userSpecificHub.run(() => {/* above example code here */})

Trying to do userSpecificHub.configureScope to set the user resulted in the second hub/scope to have no user context.

I’m probably doing something wrong but I’m trying to add some context to some background processes (most of them get started by https://github.com/kelektiv/node-cron/)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilogorekcommented, Jul 30, 2019

Do you have access to the user throughout the whole life-span of that async function and you capture errors manually? Or do you want to rely on integrations?

If former, then it should be:

const users = [
  { id: 0 },
  { id: 1 },
  { id: 2 },
  { id: 3 },
  { id: 4 },
  { id: 5 },
  { id: 6 }
];

const processUser = user => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (user.id % 2 === 0) {
        Sentry.withScope(scope => {
          scope.setUser(user);
          Sentry.captureException(new Error("whoops"));
        });
      }
      resolve();
    }, Math.random() * 1000);
  });
};

Promise.all(users.map(processUser)).then(() => {
  console.log("done");
});

However, if you want to have a separate hub for a whole life-span and modify it in various places, then this will work:

const users = [
  { id: 0 },
  { id: 1 },
  { id: 2 },
  { id: 3 },
  { id: 4 },
  { id: 5 },
  { id: 6 }
];

const processUser = user => {
  return new Promise((resolve, reject) => {
    const hub = new Sentry.Hub(Sentry.getCurrentHub().getClient());
    hub.configureScope(scope => scope.setUser(user));

    setTimeout(() => {
      if (user.id % 2 === 0) {
        hub.captureException(new Error("whoops"));
      }
      resolve();
    }, Math.random() * 1000);
  });
};

Promise.all(users.map(processUser)).then(() => {
  console.log("done");
});
0reactions
rhcarvalhocommented, Mar 13, 2020

Closing as this seems to be solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React multiple contexts - Stack Overflow
createContext('light'); // Signed-in user context const UserContext = React.createContext({ name: 'Guest', }); class App extends React.
Read more >
How do I handle multiple user contexts in an app?
Try using a modal for the search and find or create activities within the primary use case, which according to the brief description...
Read more >
Context - React
Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user,...
Read more >
How to combine context providers for cleaner React code
Context Providers are just functions therefore they can be composted, and used to create AppContextProvider lets just do that then. First, let's create...
Read more >
Manage FAST Search user contexts - Microsoft Support
Display the Manage User Context page · Log on to the home page of your top-level site with site collection administrator permissions. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found