Course contentsShow
← React
Lesson 1332 of 2,39633. Error BoundariesFree lesson

Adding Context to Error Reports

Attach user info, route data, and custom tags to error events using Sentry scopes for richer debugging context.

Adding Context to Error Reports

What you'll learn: How to attach user information, route data, and custom tags to Sentry error reports for richer debugging context.

When an error occurs in production, the stack trace tells you where the error happened, but not necessarily why or under what conditions. Adding context to your error reports is like leaving breadcrumbs for your future debugging self—it transforms a cryptic error into an actionable bug report.

The Problem with Bare Error Reports

An error saying "Cannot read property 'name' of undefined" doesn't tell you which user encountered it, what page they were on, or what actions led to the failure. That's where Sentry scopes come in.

Enriching Errors with Sentry Scopes

Sentry lets you attach metadata to errors using scope configuration. You typically do this in your error boundary's componentDidCatch or Sentry integration setup:

componentDidCatch(error, errorInfo) {
  Sentry.withScope((scope) => {
    // Attach user information
    scope.setUser({
      id: this.props.userId,
      email: this.props.userEmail
    });

    // Attach route/page context
    scope.setTag("route", window.location.pathname);
    
    // Custom tags for filtering
    scope.setTag("feature", "checkout");
    scope.setTag("environment", process.env.NODE_ENV);
    
    // Additional context
    scope.setContext("component", {
      name: errorInfo.componentStack
    });

    Sentry.captureException(error);
  });
}

Now when this error appears in Sentry, you'll see which user experienced it, on what page, and can filter by custom tags like "checkout" errors.

Key Takeaway: Use Sentry.withScope() to attach user data, route information, and custom tags to error reports—this context turns generic crashes into specific, reproducible bugs you can actually fix.